Skip to content
Browse files

Added some mutex locks to the save function

  • Loading branch information...
1 parent 75b9475 commit 9272bcd4f737ad018a6d08568b5ef854d3aef4a4 @toddtreece committed Oct 26, 2011
Showing with 17 additions and 3 deletions.
  1. +1 −0 common.h
  2. +11 −2 io.c
  3. +5 −1 server.c
View
1 common.h
@@ -20,6 +20,7 @@ typedef long long longlong;
#define VERSION_STRING_LENGTH 3
#define ERRMSG_SIZE 1000
+extern pthread_mutex_t mongodb_mutex;
extern mongo mongodb_connection[1];
#endif /* MYSQLUDF_COMMON_H */
View
13 io.c
@@ -20,13 +20,19 @@ my_bool mongodb_save_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
}
long long mongodb_save(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error) {
+
+
+ pthread_mutex_lock(&mongodb_mutex);
if(!mongodb_connection->connected) {
-
+
// Attempt to reconnect
mongo_reconnect(mongodb_connection);
}
+
+ pthread_mutex_unlock(&mongodb_mutex);
+
bson b[1];
@@ -63,15 +69,18 @@ long long mongodb_save(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned
bson_finish(b);
+ pthread_mutex_lock(&mongodb_mutex);
+
if(mongo_insert(mongodb_connection, args->args[0], b) == MONGO_ERROR && mongodb_connection->err == MONGO_IO_ERROR) {
fprintf(stderr, "saving data to mongodb failed.\n");
*error = 1;
- return 0;
}
+ pthread_mutex_unlock(&mongodb_mutex);
+
bson_destroy(b);
*is_null = 1;
View
6 server.c
@@ -1,6 +1,6 @@
#include "common.h"
-pthread_mutex_t mongodb_mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t mongodb_mutex;
mongo mongodb_connection[1];
@@ -32,6 +32,8 @@ my_bool mongodb_connect_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
long long mongodb_connect(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error) {
int rc;
+
+ pthread_mutex_init(&mongodb_mutex, NULL);
pthread_mutex_lock(&mongodb_mutex);
@@ -96,6 +98,8 @@ long long mongodb_disconnect(UDF_INIT *initid, UDF_ARGS *args, char *result, uns
pthread_mutex_unlock(&mongodb_mutex);
+ pthread_mutex_destroy(&mongodb_mutex);
+
*is_null = 1;
return 0;

0 comments on commit 9272bcd

Please sign in to comment.
Something went wrong with that request. Please try again.