Skip to content

Commit

Permalink
Cleaned up some thread things.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Aug 28, 2008
1 parent e6b4c9a commit 09cb4f2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
4 changes: 3 additions & 1 deletion mojocrash_internal.h
Expand Up @@ -85,6 +85,8 @@ int MOJOCRASH_unix_init(void);
void MOJOCRASH_unix_get_logpath(char *buf, size_t buflen, const char *appname);
void MOJOCRASH_unix_get_osver(char *buf, size_t buflen);

typedef void (*MOJOCRASH_thread_entry)(void *);

/*
* These are all functions that are platform-specific. Usually they need
* unportable system APIs, or they handle things in unique ways, or both.
Expand Down Expand Up @@ -115,7 +117,7 @@ void MOJOCRASH_platform_close_socket(void *sock);
int MOJOCRASH_platform_read_socket(void *sock, char *buf, const int l);
int MOJOCRASH_platform_write_socket(void *sock, const char *buf, const int l);
int MOJOCRASH_platform_get_http_proxy(char *buf, const int len);
int MOJOCRASH_platform_spin_thread(void (*fn)(void *), void *arg);
int MOJOCRASH_platform_spin_thread(MOJOCRASH_thread_entry fn, void *arg);

#ifdef __cplusplus
}
Expand Down
23 changes: 21 additions & 2 deletions mojocrash_unix.c
Expand Up @@ -293,12 +293,31 @@ void MOJOCRASH_platform_free_reports(const char **reports, const int total)
} /* MOJOCRASH_platform_free_reports */


int MOJOCRASH_platform_spin_thread(void (*fn)(void *), void *arg)
typedef struct
{
MOJOCRASH_thread_entry fn;
void *arg;
volatile int done;
} thread_data;

static void *thread_bridge(void *_arg)
{
thread_data *data = (thread_data *) _arg;
MOJOCRASH_thread_entry fn = data->fn;
void *arg = data->arg;
data->done = 1;
fn(arg);
return NULL;
} /* thread_bridge */

int MOJOCRASH_platform_spin_thread(MOJOCRASH_thread_entry fn, void *arg)
{
thread_data data = { fn, arg, 0 };
pthread_t thread;
if (pthread_create(&thread, NULL, (void *(*)(void *))fn, arg) != 0)
if (pthread_create(&thread, NULL, thread_bridge, arg) != 0)
return 0;
pthread_detach(thread);
while (!data.done) { /* spin. */ }
return 1;
} /* MOJOCRASH_platform_spin_thread */

Expand Down
10 changes: 5 additions & 5 deletions mojocrash_windows.c
Expand Up @@ -520,22 +520,22 @@ int MOJOCRASH_platform_get_http_proxy(char *buf, const int buflen)

typedef struct
{
void (*fn)(void *);
MOJOCRASH_thread_entry fn;
void *arg;
volatile int done;
} thread_data;

static DWORD WINAPI thread_bridge(LPVOID arg)
static DWORD WINAPI thread_bridge(LPVOID _arg)
{
thread_data *data = (thread_data *) arg;
void (*fn)(void *) = data->fn;
thread_data *data = (thread_data *) _arg;
MOJOCRASH_thread_entry fn = data->fn;
void *arg = data->arg;
data->done = 1;
fn(arg);
return 0;
} /* thread_bridge */

int MOJOCRASH_platform_spin_thread(void (*fn)(void *), void *arg)
int MOJOCRASH_platform_spin_thread(MOJOCRASH_thread_entry fn, void *arg)
{
thread_data data = { fn, arg, 0 };
HANDLE h = CreateThread(NULL, 0, thread_bridge, &data);
Expand Down

0 comments on commit 09cb4f2

Please sign in to comment.