summaryrefslogtreecommitdiffstats
path: root/native_client_sdk
diff options
context:
space:
mode:
authorbinji@chromium.org <binji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-08-23 00:03:26 +0000
committerbinji@chromium.org <binji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-08-23 00:03:26 +0000
commita8eff493c9188e0f8916aece721402ba221f575e (patch)
tree012ea230c289e1839c618d589758ed519bee3c99 /native_client_sdk
parent36a22c4003317b933133d474f49cb5f97ed37e90 (diff)
downloadchromium_src-a8eff493c9188e0f8916aece721402ba221f575e.zip
chromium_src-a8eff493c9188e0f8916aece721402ba221f575e.tar.gz
chromium_src-a8eff493c9188e0f8916aece721402ba221f575e.tar.bz2
[NaCl SDK] Added some basic functionality to pthreads_win library.
It's just enough to support gtest_ppapi, nothing more. We should consider including the pthreads-win32 library instead. http://sources.redhat.com/pthreads-win32/ BUG=none NOTRY=true Review URL: https://chromiumcodereview.appspot.com/10845005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@152883 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'native_client_sdk')
-rw-r--r--native_client_sdk/src/libraries/pthread/pthread.c96
-rw-r--r--native_client_sdk/src/libraries/pthread/pthread.h24
2 files changed, 98 insertions, 22 deletions
diff --git a/native_client_sdk/src/libraries/pthread/pthread.c b/native_client_sdk/src/libraries/pthread/pthread.c
index 68f4fc2..fd55b90 100644
--- a/native_client_sdk/src/libraries/pthread/pthread.c
+++ b/native_client_sdk/src/libraries/pthread/pthread.c
@@ -6,38 +6,98 @@
#include <errno.h>
#include "pthread.h"
-int pthread_mutex_init(pthread_mutex_t* m, void* traits) {
- *m = (int) CreateMutex(NULL, 0, NULL);
+int pthread_mutex_init(pthread_mutex_t* m, void* attrs) {
+ InitializeCriticalSection(m);
return 0;
}
int pthread_mutex_destroy(pthread_mutex_t* m) {
- CloseHandle((HANDLE) *m);
+ DeleteCriticalSection(m);
return 0;
}
int pthread_mutex_lock(pthread_mutex_t* m) {
- if (WaitForSingleObject((HANDLE) *m, INFINITE) == WAIT_OBJECT_0)
+ EnterCriticalSection(m);
+ return 0;
+}
+
+int pthread_mutex_unlock(pthread_mutex_t* m) {
+ LeaveCriticalSection(m);
+ return 0;
+}
+
+int pthread_mutex_trylock(pthread_mutex_t* m) {
+ if (TryEnterCriticalSection(m))
return 0;
+ return EBUSY;
+}
- return EINVAL;
+int pthread_cond_init(pthread_cond_t* c, void* attrs) {
+ InitializeConditionVariable(c);
+ return 0;
}
-int pthread_mutex_unlock(pthread_mutex_t* m) {
- if (ReleaseMutex((HANDLE) *m)) return 0;
+int pthread_cond_destroy(pthread_cond_t* c) {
+ return 0;
+}
+int pthread_cond_broadcast(pthread_cond_t* c) {
+ WakeAllConditionVariable(c);
+ return 0;
+}
+
+int pthread_cond_signal(pthread_cond_t* c) {
+ WakeConditionVariable(c);
+ return 0;
+}
+
+int pthread_cond_wait(pthread_cond_t* c, pthread_mutex_t* m) {
+ if (SleepConditionVariableCS(c, m, INFINITE))
+ return 0;
return EINVAL;
}
-int pthread_mutex_trylock(pthread_mutex_t* m) {
- int val = WaitForSingleObject((HANDLE) *m, 0);
-
- if (val == WAIT_OBJECT_0) return 0;
-
- if (val == WAIT_TIMEOUT) {
- errno = EBUSY;
- } else {
- errno = EINVAL;
+typedef struct {
+ void* (*start_routine)(void*);
+ void* arg;
+} PthreadCreateInfo;
+
+static DWORD WINAPI PthreadCreateThreadFunc(LPVOID param) {
+ PthreadCreateInfo* pthread_create_info = (PthreadCreateInfo*)param;
+ void* result = (pthread_create_info->start_routine)(pthread_create_info->arg);
+ (void)result; // Ignore result
+ HeapFree(GetProcessHeap(), 0, pthread_create_info);
+ return 0;
+}
+
+int pthread_create(pthread_t* t,
+ void* attrs,
+ void* (*start_routine)(void*),
+ void* arg) {
+ HANDLE thread_handle;
+ PthreadCreateInfo* pthread_create_info;
+
+ pthread_create_info = (PthreadCreateInfo*)HeapAlloc(
+ GetProcessHeap(),
+ 0,
+ sizeof(PthreadCreateInfo));
+ if (pthread_create_info == NULL)
+ return EAGAIN;
+
+ pthread_create_info->start_routine = start_routine;
+ pthread_create_info->arg = arg;
+
+ thread_handle = CreateThread(NULL, // lpThreadAttributes
+ 0, // dwStackSize
+ &PthreadCreateThreadFunc, // lpStartAddress
+ pthread_create_info, // lpParameter,
+ 0, // dwCreationFlags
+ NULL); // lpThreadId
+ if (thread_handle == NULL) {
+ HeapFree(GetProcessHeap(), 0, pthread_create_info);
+ return EAGAIN;
}
- return -1;
-} \ No newline at end of file
+
+ *t = thread_handle;
+ return 0;
+}
diff --git a/native_client_sdk/src/libraries/pthread/pthread.h b/native_client_sdk/src/libraries/pthread/pthread.h
index 6647861..c9125bf 100644
--- a/native_client_sdk/src/libraries/pthread/pthread.h
+++ b/native_client_sdk/src/libraries/pthread/pthread.h
@@ -10,20 +10,36 @@
* Implementation of pthread.h for building the SDK natively on Windows.
*/
+#include <windows.h>
+#include <time.h>
+
#ifdef __cplusplus
extern "C" {
#endif
-typedef int pthread_mutex_t;
+typedef HANDLE pthread_t;
+typedef CRITICAL_SECTION pthread_mutex_t;
+typedef CONDITION_VARIABLE pthread_cond_t;
-int pthread_mutex_init(pthread_mutex_t* m, void* traits);
+int pthread_mutex_init(pthread_mutex_t* m, void* attrs);
int pthread_mutex_destroy(pthread_mutex_t* m);
-
int pthread_mutex_lock(pthread_mutex_t* m);
int pthread_mutex_unlock(pthread_mutex_t* m);
+int pthread_mutex_trylock(pthread_mutex_t* m);
+
+int pthread_cond_init(pthread_cond_t* c, void* attrs);
+int pthread_cond_destroy(pthread_cond_t* c);
+int pthread_cond_broadcast(pthread_cond_t* c);
+int pthread_cond_signal(pthread_cond_t* c);
+int pthread_cond_wait(pthread_cond_t* c, pthread_mutex_t* m);
+
+int pthread_create(pthread_t* t,
+ void* attrs,
+ void* (*start_routine)(void*),
+ void* arg);
#ifdef __cplusplus
}
#endif
-#endif /* LIBRARIES_PTHREAD_PTHREAD_H_ */ \ No newline at end of file
+#endif /* LIBRARIES_PTHREAD_PTHREAD_H_ */