Skip to content

Commit

Permalink
pthread: (partial) merge once code from redhat win32 pthread.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.netlabs.org/repos/ports/pthread/trunk@224 8d6eb4b1-0faa-de11-b929-00e081727c95
  • Loading branch information
ydario committed Nov 24, 2010
1 parent e4528d9 commit 171b54d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
26 changes: 18 additions & 8 deletions src/my_os2thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <string.h>
#include <process.h>
#include <types.h>
#include <sys/builtin.h>

#include "pthread.h"

#undef pthread_exit
Expand Down Expand Up @@ -166,16 +168,24 @@ int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
{
pthread_mutex_t THR_LOCK_once;

// protect init
pthread_mutex_lock(&THR_LOCK_once);
if (once_control == NULL || init_routine == NULL)
{
return EINVAL;
}

if (*once_control == (pthread_once_t)PTHREAD_ONCE_INIT)
{
*once_control = 0;
init_routine();
}
if (!__atomic_xchg((unsigned)&once_control->done, 0)) /* MBR fence */
{
pthread_mutex_lock(&once_control->lock);

if (!once_control->done)
{
(*init_routine)();
once_control->done = PTW32_TRUE;
}

pthread_mutex_unlock(&once_control->lock);
}

pthread_mutex_unlock(&THR_LOCK_once);
return 0;
}

Expand Down
32 changes: 28 additions & 4 deletions src/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
extern "C" {
#endif

/*
* Boolean values to make us independent of system includes.
*/
enum {
PTW32_FALSE = 0,
PTW32_TRUE = (! PTW32_FALSE)
};

/*
* Flags for once initialization.
*/
Expand Down Expand Up @@ -85,10 +93,6 @@ void pthread_setprio( int, int);
#define pthread_condattr_init(A) 0
#define pthread_condattr_destroy(A) 0

typedef int* pthread_once_t;
#define PTHREAD_ONCE_INIT ((pthread_once_t)-1)
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));

int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);

Expand Down Expand Up @@ -206,6 +210,26 @@ int pthread_setcanceltype(int type, int *oldtype);
void pthread_testcancel(void);
int pthread_atfork(void (*prepare)(void), void (*parent)(void),void (*child)(void));

/*
* ====================
* ====================
* Once Key
* ====================
* ====================
*/
#define PTHREAD_ONCE_INIT { PTW32_FALSE, 0, 0, 0}

struct pthread_once_t_
{
unsigned done; /* indicates if user function has been executed */
pthread_mutex_t lock;
int reserved1;
int reserved2;
};
typedef struct pthread_once_t_ pthread_once_t;
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));


#ifdef __cplusplus
} // extern "C"
#endif
Expand Down

0 comments on commit 171b54d

Please sign in to comment.