Permalink
Browse files

* Finish pthread_* => uv_* porting.

  Now compiles on Win32, although doesn't yet work properly.
(cherry picked from commit 569ddd126560f9e4e11a580241d1757434cd238f)

Conflicts:

	src/threads_a_gogo.cc
  • Loading branch information...
1 parent aa12a25 commit 9dcae9d5d0c2d864cb2333cc581bb7cb69ecd9de @audreyt committed Nov 10, 2012
Showing with 30 additions and 19 deletions.
  1. +10 −8 src/queues_a_gogo.cc
  2. +20 −11 src/threads_a_gogo.cc
View
18 src/queues_a_gogo.cc 100755 → 100644
@@ -1,11 +1,13 @@
//2011-11 Proyectos Equis Ka, s.l., jorge@jorgechamorro.com
//queues_a_gogo.cc
-#include <unistd.h>
#include <string.h>
#include <stdio.h>
-#include <pthread.h>
+#include <uv.h>
#include <stdlib.h>
+#if defined(__unix__) || defined(__POSIX__) || defined(__APPLE__) || defined(_AIX)
+#include <unistd.h>
+#endif
enum types {
kItemTypeNONE,
@@ -27,7 +29,7 @@ typedef struct typeQueueItem typeQueueItem;
typedef struct {
typeQueueItem* first;
typeQueueItem* last;
- pthread_mutex_t queueLock;
+ uv_mutex_t queueLock;
long int id;
volatile long int length;
} typeQueue;
@@ -41,7 +43,7 @@ static typeQueue* freeItemsQueue= NULL;
static void queue_push (typeQueueItem* qitem, typeQueue* queue) {
qitem->next= NULL;
- pthread_mutex_lock(&queue->queueLock);
+ uv_mutex_lock(&queue->queueLock);
if (queue->last) {
queue->last->next= qitem;
}
@@ -50,7 +52,7 @@ static void queue_push (typeQueueItem* qitem, typeQueue* queue) {
}
queue->last= qitem;
queue->length++;
- pthread_mutex_unlock(&queue->queueLock);
+ uv_mutex_unlock(&queue->queueLock);
}
@@ -59,7 +61,7 @@ static void queue_push (typeQueueItem* qitem, typeQueue* queue) {
static typeQueueItem* queue_pull (typeQueue* queue) {
typeQueueItem* qitem;
- pthread_mutex_lock(&queue->queueLock);
+ uv_mutex_lock(&queue->queueLock);
if ((qitem= queue->first)) {
if (queue->last == qitem) {
queue->first= queue->last= NULL;
@@ -70,7 +72,7 @@ static typeQueueItem* queue_pull (typeQueue* queue) {
queue->length--;
qitem->next= NULL;
}
- pthread_mutex_unlock(&queue->queueLock);
+ uv_mutex_unlock(&queue->queueLock);
return qitem;
}
@@ -124,7 +126,7 @@ static typeQueue* nuQueue (long int id) {
}
else {
queue= (typeQueue*) calloc(1, sizeof(typeQueue));
- pthread_mutex_init(&queue->queueLock, NULL);
+ uv_mutex_init(&queue->queueLock);
}
queue->id= id;
View
31 src/threads_a_gogo.cc 100755 → 100644
@@ -5,14 +5,21 @@
#include <v8.h>
#include <node.h>
#include <uv.h>
-#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#if defined(__unix__) || defined(__POSIX__) || defined(__APPLE__) || defined(_AIX)
+#define WWT_PTHREAD 1
#include <pthread.h>
+#include <unistd.h>
+#ifndef uv_cond_t
+#define uv_cond_signal(x) pthread_cond_signal(x)
+#define uv_cond_init(x) pthread_cond_init(x, NULL)
+#define uv_cond_wait(x,y) pthread_cond_wait(x, y)
+typedef pthread_cond_t uv_cond_t;
+#endif
#else
#define pthread_setcancelstate(x,y) NULL
#define pthread_setcanceltype(x,y) NULL
@@ -35,13 +42,6 @@ static bool useLocker;
static typeQueue* freeJobsQueue= NULL;
static typeQueue* freeThreadsQueue= NULL;
-#ifndef uv_cond_t
-#define uv_cond_signal(x) pthread_cond_signal(x)
-#define uv_cond_init(x) pthread_cond_init(x, NULL)
-#define uv_cond_wait(x,y) pthread_cond_wait(x, y)
-typedef pthread_cond_t uv_cond_t;
-#endif
-
#define kThreadMagicCookie 0x99c0ffee
typedef struct {
uv_async_t async_watcher; //MUST be the first one
@@ -204,8 +204,12 @@ static Handle<Value> Print (const Arguments &args) {
static void eventLoop (typeThread* thread);
// A background thread
+#ifdef WWT_PTHREAD
static void* aThread (void* arg) {
-
+#else
+static void aThread (void* arg) {
+#endif
+
int dummy;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &dummy);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &dummy);
@@ -230,8 +234,9 @@ static void* aThread (void* arg) {
// wake up callback
if (!(thread->inQueue.length)) uv_async_send(&thread->async_watcher);
-
+#ifdef WWT_PTHREAD
return NULL;
+#endif
}
@@ -719,12 +724,16 @@ static Handle<Value> Create (const Arguments &args) {
uv_mutex_init(&thread->IDLE_mutex);
uv_mutex_init(&thread->inQueue.queueLock);
uv_mutex_init(&thread->outQueue.queueLock);
+
+#ifdef WWT_PTHREAD
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
int err= pthread_create(&thread->thread, &attr, aThread, thread);
pthread_attr_destroy(&attr);
- // int err= uv_thread_create(&thread->thread, aThread, thread);
+#else
+ int err= uv_thread_create(&thread->thread, aThread, thread);
+#endif
if (err) {
//Ha habido un error no se ha arrancado esta thread
destroyaThread(thread);

0 comments on commit 9dcae9d

Please sign in to comment.