mirror of
https://github.com/kingk85/uFTP.git
synced 2025-04-20 00:18:57 +03:00
Working on mutex!
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
build/uFTP
BIN
build/uFTP
Binary file not shown.
@ -632,12 +632,13 @@ int parseCommandList(ftpDataType * data, int socketId)
|
||||
setDynamicStringDataType(&data->clients[socketId].listPath, data->clients[socketId].login.absolutePath.text, data->clients[socketId].login.absolutePath.textLen, &data->clients[socketId].memoryTable);
|
||||
}
|
||||
|
||||
//pthread_mutex_trylock(&data->clients[socketId].workerData.conditionMutex);
|
||||
pthread_mutex_lock(&data->clients[socketId].conditionMutex);
|
||||
memset(data->clients[socketId].workerData.theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE);
|
||||
strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived);
|
||||
data->clients[socketId].workerData.commandReceived = 1;
|
||||
//pthread_mutex_unlock(&data->clients[socketId].workerData.conditionMutex);
|
||||
pthread_cond_signal(&data->clients[socketId].workerData.conditionVariable);
|
||||
pthread_cond_signal(&data->clients[socketId].conditionVariable);
|
||||
pthread_mutex_unlock(&data->clients[socketId].conditionMutex);
|
||||
|
||||
return FTP_COMMAND_PROCESSED;
|
||||
}
|
||||
|
||||
@ -663,12 +664,14 @@ int parseCommandNlst(ftpDataType * data, int socketId)
|
||||
setDynamicStringDataType(&data->clients[socketId].nlistPath, data->clients[socketId].login.absolutePath.text, data->clients[socketId].login.absolutePath.textLen, &data->clients[socketId].memoryTable);
|
||||
}
|
||||
|
||||
//pthread_mutex_trylock(&data->clients[socketId].workerData.conditionMutex);
|
||||
pthread_mutex_lock(&data->clients[socketId].conditionMutex);
|
||||
|
||||
memset(data->clients[socketId].workerData.theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE);
|
||||
strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived);
|
||||
data->clients[socketId].workerData.commandReceived = 1;
|
||||
//pthread_mutex_unlock(&data->clients[socketId].workerData.conditionMutex);
|
||||
pthread_cond_signal(&data->clients[socketId].workerData.conditionVariable);
|
||||
pthread_cond_signal(&data->clients[socketId].conditionVariable);
|
||||
pthread_mutex_unlock(&data->clients[socketId].conditionMutex);
|
||||
|
||||
return FTP_COMMAND_PROCESSED;
|
||||
}
|
||||
|
||||
@ -688,12 +691,14 @@ int parseCommandRetr(ftpDataType * data, int socketId)
|
||||
if (isSafePath == 1 &&
|
||||
FILE_IsFile(data->clients[socketId].fileToRetr.text) == 1)
|
||||
{
|
||||
//pthread_mutex_trylock(&data->clients[socketId].workerData.conditionMutex);
|
||||
pthread_mutex_lock(&data->clients[socketId].conditionMutex);
|
||||
|
||||
memset(data->clients[socketId].workerData.theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE);
|
||||
strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived);
|
||||
data->clients[socketId].workerData.commandReceived = 1;
|
||||
//pthread_mutex_unlock(&data->clients[socketId].workerData.conditionMutex);
|
||||
pthread_cond_signal(&data->clients[socketId].workerData.conditionVariable);
|
||||
pthread_cond_signal(&data->clients[socketId].conditionVariable);
|
||||
pthread_mutex_unlock(&data->clients[socketId].conditionMutex);
|
||||
|
||||
return FTP_COMMAND_PROCESSED;
|
||||
}
|
||||
else
|
||||
@ -718,12 +723,13 @@ int parseCommandStor(ftpDataType * data, int socketId)
|
||||
|
||||
if (isSafePath == 1)
|
||||
{
|
||||
//pthread_mutex_trylock(&data->clients[socketId].workerData.conditionMutex);
|
||||
pthread_mutex_lock(&data->clients[socketId].conditionMutex);
|
||||
memset(data->clients[socketId].workerData.theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE);
|
||||
strcpy(data->clients[socketId].workerData.theCommandReceived, data->clients[socketId].theCommandReceived);
|
||||
data->clients[socketId].workerData.commandReceived = 1;
|
||||
//pthread_mutex_unlock(&data->clients[socketId].workerData.conditionMutex);
|
||||
pthread_cond_signal(&data->clients[socketId].workerData.conditionVariable);
|
||||
pthread_cond_signal(&data->clients[socketId].conditionVariable);
|
||||
pthread_mutex_unlock(&data->clients[socketId].conditionMutex);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
35
ftpData.c
35
ftpData.c
@ -593,9 +593,6 @@ void resetWorkerData(ftpDataType *data, int clientId, int isInitialization)
|
||||
/* wait main for action */
|
||||
if (isInitialization != 1)
|
||||
{
|
||||
pthread_mutex_destroy(&data->clients[clientId].workerData.conditionMutex);
|
||||
pthread_cond_destroy(&data->clients[clientId].workerData.conditionVariable);
|
||||
|
||||
if (data->clients[clientId].workerData.theStorFile != NULL)
|
||||
{
|
||||
fclose(data->clients[clientId].workerData.theStorFile);
|
||||
@ -614,18 +611,6 @@ void resetWorkerData(ftpDataType *data, int clientId, int isInitialization)
|
||||
data->clients[clientId].workerData.threadHasBeenCreated = 0;
|
||||
}
|
||||
|
||||
if (pthread_mutex_init(&data->clients[clientId].workerData.conditionMutex, NULL) != 0)
|
||||
{
|
||||
// printf("\ndata->clients[clientId].workerData.conditionMutex init failed\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
if (pthread_cond_init(&data->clients[clientId].workerData.conditionVariable, NULL) != 0)
|
||||
{
|
||||
// printf("\ndata->clients[clientId].workerData.conditionVariable init failed\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
//Clear the dynamic vector structure
|
||||
int theSize = data->clients[clientId].workerData.directoryInfo.Size;
|
||||
@ -645,12 +630,14 @@ void resetWorkerData(ftpDataType *data, int clientId, int isInitialization)
|
||||
|
||||
void resetClientData(ftpDataType *data, int clientId, int isInitialization)
|
||||
{
|
||||
|
||||
if (isInitialization != 1)
|
||||
{
|
||||
if (data->clients[clientId].workerData.threadIsAlive == 1)
|
||||
pthread_cancel(data->clients[clientId].workerData.workerThread);
|
||||
|
||||
pthread_mutex_destroy(&data->clients[clientId].conditionMutex);
|
||||
pthread_cond_destroy(&data->clients[clientId].conditionVariable);
|
||||
|
||||
pthread_mutex_destroy(&data->clients[clientId].writeMutex);
|
||||
|
||||
#ifdef OPENSSL_ENABLED
|
||||
@ -658,6 +645,10 @@ void resetClientData(ftpDataType *data, int clientId, int isInitialization)
|
||||
//SSL_free(data->clients[clientId].workerData.ssl);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
if (pthread_mutex_init(&data->clients[clientId].writeMutex, NULL) != 0)
|
||||
{
|
||||
@ -665,6 +656,18 @@ void resetClientData(ftpDataType *data, int clientId, int isInitialization)
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (pthread_mutex_init(&data->clients[clientId].conditionMutex, NULL) != 0)
|
||||
{
|
||||
printf("\ndata->clients[clientId].workerData.conditionMutex init failed\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (pthread_cond_init(&data->clients[clientId].conditionVariable, NULL) != 0)
|
||||
{
|
||||
printf("\ndata->clients[clientId].workerData.conditionVariable init failed\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
data->clients[clientId].tlsIsNegotiating = 0;
|
||||
data->clients[clientId].tlsIsEnabled = 0;
|
||||
data->clients[clientId].dataChannelIsTls = 0;
|
||||
|
@ -148,8 +148,6 @@ struct workerData
|
||||
long long int retrRestartAtByte;
|
||||
|
||||
/* The PASV thread will wait the signal before start */
|
||||
pthread_mutex_t conditionMutex;
|
||||
pthread_cond_t conditionVariable;
|
||||
ftpCommandDataType ftpCommand;
|
||||
DYNV_VectorGenericDataType directoryInfo;
|
||||
FILE *theStorFile;
|
||||
@ -207,6 +205,9 @@ struct clientData
|
||||
unsigned long long int connectionTimeStamp;
|
||||
unsigned long long int lastActivityTimeStamp;
|
||||
|
||||
pthread_mutex_t conditionMutex;
|
||||
pthread_cond_t conditionVariable;
|
||||
|
||||
DYNMEM_MemoryTable_DataType *memoryTable;
|
||||
} typedef clientDataType;
|
||||
|
||||
|
@ -264,12 +264,12 @@ void *connectionWorkerHandle(void * socketId)
|
||||
{
|
||||
//printf("\nWorker %d is waiting for commands!", theSocketId);
|
||||
//Conditional lock on tconditionVariablehread actions
|
||||
pthread_mutex_lock(&ftpData.clients[theSocketId].workerData.conditionMutex);
|
||||
pthread_mutex_lock(&ftpData.clients[theSocketId].conditionMutex);
|
||||
while (ftpData.clients[theSocketId].workerData.commandReceived == 0)
|
||||
{
|
||||
pthread_cond_wait(&ftpData.clients[theSocketId].workerData.conditionVariable, &ftpData.clients[theSocketId].workerData.conditionMutex);
|
||||
pthread_cond_wait(&ftpData.clients[theSocketId].conditionVariable, &ftpData.clients[theSocketId].conditionMutex);
|
||||
}
|
||||
pthread_mutex_unlock(&ftpData.clients[theSocketId].workerData.conditionMutex);
|
||||
pthread_mutex_unlock(&ftpData.clients[theSocketId].conditionMutex);
|
||||
|
||||
//printf("\nWorker %d unlocked", theSocketId);
|
||||
|
||||
|
Reference in New Issue
Block a user