src/thread.c
changeset 5475 2e6990a8c7c4
parent 4324 9682c016b892
equal deleted inserted replaced
5474:ac55aefc54f3 5475:2e6990a8c7c4
       
     1 /* $Id$ */
       
     2 
       
     3 #include "stdafx.h"
       
     4 #include "thread.h"
       
     5 #include <stdlib.h>
       
     6 
       
     7 #if defined(__AMIGA__) || defined(__MORPHOS__) || defined(NO_THREADS)
       
     8 OTTDThread *OTTDCreateThread(OTTDThreadFunc function, void *arg) { return NULL; }
       
     9 void *OTTDJoinThread(OTTDThread *t) { return NULL; }
       
    10 void OTTDExitThread(void) { NOT_REACHED(); };
       
    11 
       
    12 #elif defined(__OS2__)
       
    13 
       
    14 #define INCL_DOS
       
    15 #include <os2.h>
       
    16 #include <process.h>
       
    17 
       
    18 struct OTTDThread {
       
    19 	TID thread;
       
    20 	OTTDThreadFunc func;
       
    21 	void* arg;
       
    22 	void* ret;
       
    23 };
       
    24 
       
    25 static void Proxy(void* arg)
       
    26 {
       
    27 	OTTDThread* t = arg;
       
    28 	t->ret = t->func(t->arg);
       
    29 }
       
    30 
       
    31 OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg)
       
    32 {
       
    33 	OTTDThread* t = malloc(sizeof(*t));
       
    34 
       
    35 	if (t == NULL) return NULL;
       
    36 
       
    37 	t->func = function;
       
    38 	t->arg  = arg;
       
    39 	t->thread = _beginthread(Proxy, NULL, 32768, t);
       
    40 	if (t->thread != -1) {
       
    41 		return t;
       
    42 	} else {
       
    43 		free(t);
       
    44 		return NULL;
       
    45 	}
       
    46 }
       
    47 
       
    48 void* OTTDJoinThread(OTTDThread* t)
       
    49 {
       
    50 	void* ret;
       
    51 
       
    52 	if (t == NULL) return NULL;
       
    53 
       
    54 	DosWaitThread(&t->thread, DCWW_WAIT);
       
    55 	ret = t->ret;
       
    56 	free(t);
       
    57 	return ret;
       
    58 }
       
    59 
       
    60 void OTTDExitThread(void)
       
    61 {
       
    62 	_endthread();
       
    63 }
       
    64 
       
    65 #elif defined(UNIX)
       
    66 
       
    67 #include <pthread.h>
       
    68 
       
    69 struct OTTDThread {
       
    70 	pthread_t thread;
       
    71 };
       
    72 
       
    73 OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg)
       
    74 {
       
    75 	OTTDThread* t = malloc(sizeof(*t));
       
    76 
       
    77 	if (t == NULL) return NULL;
       
    78 
       
    79 	if (pthread_create(&t->thread, NULL, function, arg) == 0) {
       
    80 		return t;
       
    81 	} else {
       
    82 		free(t);
       
    83 		return NULL;
       
    84 	}
       
    85 }
       
    86 
       
    87 void* OTTDJoinThread(OTTDThread* t)
       
    88 {
       
    89 	void* ret;
       
    90 
       
    91 	if (t == NULL) return NULL;
       
    92 
       
    93 	pthread_join(t->thread, &ret);
       
    94 	free(t);
       
    95 	return ret;
       
    96 }
       
    97 
       
    98 void OTTDExitThread(void)
       
    99 {
       
   100 	pthread_exit(NULL);
       
   101 }
       
   102 
       
   103 #elif defined(WIN32)
       
   104 
       
   105 #include <windows.h>
       
   106 
       
   107 struct OTTDThread {
       
   108 	HANDLE thread;
       
   109 	OTTDThreadFunc func;
       
   110 	void* arg;
       
   111 	void* ret;
       
   112 };
       
   113 
       
   114 static DWORD WINAPI Proxy(LPVOID arg)
       
   115 {
       
   116 	OTTDThread* t = arg;
       
   117 	t->ret = t->func(t->arg);
       
   118 	return 0;
       
   119 }
       
   120 
       
   121 OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg)
       
   122 {
       
   123 	OTTDThread* t = malloc(sizeof(*t));
       
   124 	DWORD dwThreadId;
       
   125 
       
   126 	if (t == NULL) return NULL;
       
   127 
       
   128 	t->func = function;
       
   129 	t->arg  = arg;
       
   130 	t->thread = CreateThread(NULL, 0, Proxy, t, 0, &dwThreadId);
       
   131 
       
   132 	if (t->thread != NULL) {
       
   133 		return t;
       
   134 	} else {
       
   135 		free(t);
       
   136 		return NULL;
       
   137 	}
       
   138 }
       
   139 
       
   140 void* OTTDJoinThread(OTTDThread* t)
       
   141 {
       
   142 	void* ret;
       
   143 
       
   144 	if (t == NULL) return NULL;
       
   145 
       
   146 	WaitForSingleObject(t->thread, INFINITE);
       
   147 	CloseHandle(t->thread);
       
   148 	ret = t->ret;
       
   149 	free(t);
       
   150 	return ret;
       
   151 }
       
   152 
       
   153 void OTTDExitThread(void)
       
   154 {
       
   155 	ExitThread(0);
       
   156 }
       
   157 #endif