MAGMA  magma-1.4.0
Matrix Algebra on GPU and Multicore Architectures
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
magmawinthread.c File Reference
#include "magmawinthread.h"
#include <limits.h>
#include <process.h>
#include <stdio.h>
#include <magma.h>
Include dependency graph for magmawinthread.c:

Go to the source code of this file.

Functions

MAGMA_DLLPORT unsigned int
MAGMA_CDECL 
pthread_self_id (void)
 
MAGMA_DLLPORT pthread_t MAGMA_CDECL pthread_self (void)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_equal (pthread_t thread1, pthread_t thread2)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_mutex_init (pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
 
static int pthread_mutex_check_for_static_initialization (pthread_mutex_t *mutex)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_mutex_lock (pthread_mutex_t *mutex)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_mutex_trylock (pthread_mutex_t *mutex)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_mutex_unlock (pthread_mutex_t *mutex)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_mutex_destroy (pthread_mutex_t *mutex)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_attr_init (pthread_attr_t *attr)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_attr_destroy (pthread_attr_t *attr)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_attr_setscope (pthread_attr_t *attr, int scope)
 
unsigned WINAPI MAGMA_winThStart (void *arg)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_create (pthread_t *thread, const pthread_attr_t *attr, void *(*start)(void *), void *arg)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_join (pthread_t thread, void **value_ptr)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_cond_init (pthread_cond_t *cond, const pthread_condattr_t *attr)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_cond_destroy (pthread_cond_t *cond)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_cond_broadcast (pthread_cond_t *cond)
 
MAGMA_DLLPORT int MAGMA_CDECL pthread_setconcurrency (int level)
 

Variables

CRITICAL_SECTION magmawinthread_static_initializer_check_lock
 
static int magmawinthread_initialized = 0
 
void *(* MAGMA_realThStart )(void *)
 
int pthread_conclevel
 

Function Documentation

unsigned WINAPI MAGMA_winThStart ( void *  arg)

Definition at line 147 of file magmawinthread.c.

References MAGMA_realThStart.

147  {
148  MAGMA_realThStart( arg );
149  return 0;
150 }
void *(* MAGMA_realThStart)(void *)

Here is the caller graph for this function:

MAGMA_DLLPORT int MAGMA_CDECL pthread_attr_destroy ( pthread_attr_t attr)

Definition at line 127 of file magmawinthread.c.

127  {
128  *attr = 0;
129  return 0;
130 }

Here is the caller graph for this function:

MAGMA_DLLPORT int MAGMA_CDECL pthread_attr_init ( pthread_attr_t attr)

Definition at line 122 of file magmawinthread.c.

122  {
123  *attr = 1;
124  return 0;
125 }

Here is the caller graph for this function:

MAGMA_DLLPORT int MAGMA_CDECL pthread_attr_setscope ( pthread_attr_t attr,
int  scope 
)

Definition at line 132 of file magmawinthread.c.

132  {
133  if (*attr != 1)
134  return -1;
135 
136  if (scope != PTHREAD_SCOPE_SYSTEM)
137  return -1;
138 
139  return 0;
140 }
#define PTHREAD_SCOPE_SYSTEM

Here is the caller graph for this function:

MAGMA_DLLPORT int MAGMA_CDECL pthread_cond_broadcast ( pthread_cond_t cond)

Definition at line 239 of file magmawinthread.c.

239  {
240  int more_waiters = 0;
241 
242  /* This is needed to ensure exclusive access to "waitCount" */
243  EnterCriticalSection (&cond->cs);
244 
245  if (cond->waitCount > 0) {
246  /* always are broadcasting - no need for pthread_cond_singal() case */
247  more_waiters = 1;
248  }
249 
250  if (more_waiters) {
251  /* this will wake up all the waiters atomically at once. */
252  ReleaseSemaphore(cond->hSem, cond->waitCount, 0);
253 
254  LeaveCriticalSection(&cond->cs);
255 
256  /* Wait for all the awakened threads to acquire the counting semaphore. */
257  WaitForSingleObject(cond->hEvt, INFINITE);
258  } else
259  LeaveCriticalSection(&cond->cs);
260 
261  return 0;
262 }
CRITICAL_SECTION cs

Here is the caller graph for this function:

MAGMA_DLLPORT int MAGMA_CDECL pthread_cond_destroy ( pthread_cond_t cond)

Definition at line 197 of file magmawinthread.c.

197  {
198  DeleteCriticalSection( &cond->cs );
199  CloseHandle( cond->hSem );
200  CloseHandle( cond->hEvt );
201  return 0;
202 }
CRITICAL_SECTION cs
MAGMA_DLLPORT int MAGMA_CDECL pthread_cond_init ( pthread_cond_t cond,
const pthread_condattr_t attr 
)

Definition at line 183 of file magmawinthread.c.

183  {
184  InitializeCriticalSection( &cond->cs );
185  cond->hSem = CreateSemaphore( NULL, /* no security attributes */
186  0, /* initial count */
187  LONG_MAX, /* maximum count*/
188  NULL ); /* unnamed semaphore */
189  cond->hEvt = CreateEvent( NULL, /* no security attributes */
190  FALSE, /* reset to not-singaled automatically */
191  FALSE, /* set initial status to not-signaled */
192  NULL ); /* unnamed event */
193  cond->waitCount = 0;
194  return 0;
195 }
Definition: quark.c:94
CRITICAL_SECTION cs

Here is the caller graph for this function:

MAGMA_DLLPORT int MAGMA_CDECL pthread_cond_wait ( pthread_cond_t cond,
pthread_mutex_t mutex 
)

Definition at line 204 of file magmawinthread.c.

204  {
205  int last;
206 
208 
209  /* Avoid race condition on waiting thread counter. */
210  EnterCriticalSection(&cond->cs);
211  cond->waitCount++;
212  LeaveCriticalSection(&cond->cs);
213 
214  /* Releases _atomically_ the mutex and wait on the semaphore until
215  pthread_cond_signal() or pthread_cond_broadcast() are called (by another thread). */
216  SignalObjectAndWait(*mutex, cond->hSem, INFINITE, FALSE);
217 
218  /* Avoid race condition on waiting thread counter. */
219  EnterCriticalSection(&cond->cs);
220  cond->waitCount--; /* this thread doesn't wait any more */
221 
222  /* if this is the last thread to have waited */
223  last = cond->waitCount == 0;
224 
225  LeaveCriticalSection(&cond->cs);
226 
227  /* If this thread is the last waiter thread during this particular broadcast
228  then let all the other threads proceed. */
229  if (last)
230  /* This call ensures that two things happen atomically: signaling the hEvt event and
231  waiting until "mutex" can be acquired. */
232  SignalObjectAndWait(cond->hEvt, *mutex, INFINITE, FALSE);
233  else
234  WaitForSingleObject(*mutex, INFINITE); /* Upon return, this thread has to own "mutex". */
235 
236  return 0;
237 }
static int pthread_mutex_check_for_static_initialization(pthread_mutex_t *mutex)
#define PTHREAD_MUTEX_INITIALIZER
Definition: quark.c:94
CRITICAL_SECTION cs

Here is the caller graph for this function:

MAGMA_DLLPORT int MAGMA_CDECL pthread_create ( pthread_t thread,
const pthread_attr_t attr,
void *(*)(void *)  start,
void *  arg 
)

Definition at line 152 of file magmawinthread.c.

152  {
153 
154  /* this assumes that the threads call the same function, always; it also assumes there
155  is no race condition while assigning a pointer and using it from within threads
156  (this assumption is fulfilled by creating the new thread in suspended state) */
157  MAGMA_realThStart = start;
158 
159  thread->hThread = (HANDLE)_beginthreadex(
160  NULL, /* default security */
161  0, /* stack size: use the size of calling thread */
163  arg,
164  CREATE_SUSPENDED,
165  /*0,*/ /* the thread will run immedietally (rather than get suspended) */
166  &thread->uThId );
167 
168  /* We need to make sure that _beginthreadex() returns to the parent thread first
169  so we can safely fill up the members of the pthread_t structure without possible
170  race conditions. If the new thread is created in supsended state we eliminate
171  the race condition but now we have to resume the new thread. */
172  ResumeThread( thread->hThread );
173 
174  return 0;
175 }
HANDLE hThread
unsigned int uThId
void *(* MAGMA_realThStart)(void *)
unsigned WINAPI MAGMA_winThStart(void *arg)

Here is the caller graph for this function:

MAGMA_DLLPORT int MAGMA_CDECL pthread_equal ( pthread_t  thread1,
pthread_t  thread2 
)

Definition at line 42 of file magmawinthread.c.

42  {
43  if (thread1.uThId == thread2.uThId) // && thread1.hThread == thread2.hThread)
44  return 1;
45  return 0;
46 }
unsigned int uThId

Here is the caller graph for this function:

MAGMA_DLLPORT int MAGMA_CDECL pthread_join ( pthread_t  thread,
void **  value_ptr 
)

Definition at line 177 of file magmawinthread.c.

177  {
178  WaitForSingleObject( thread.hThread, INFINITE );
179  CloseHandle( thread.hThread );
180  return 0;
181 }
HANDLE hThread

Here is the caller graph for this function:

static int pthread_mutex_check_for_static_initialization ( pthread_mutex_t mutex)
static

Definition at line 58 of file magmawinthread.c.

References magmawinthread_initialized, magmawinthread_static_initializer_check_lock, pthread_mutex_init(), and PTHREAD_MUTEX_INITIALIZER.

58  {
59  int retval = 0;
60  /* This should be called once to initialize some structures */
61  if ( magmawinthread_initialized == 0 ) {
63  InitializeCriticalSection( &magmawinthread_static_initializer_check_lock );
64  }
65  EnterCriticalSection( &magmawinthread_static_initializer_check_lock );
66  if ( *mutex == PTHREAD_MUTEX_INITIALIZER )
67  retval = pthread_mutex_init( mutex, NULL );
68  LeaveCriticalSection( &magmawinthread_static_initializer_check_lock );
69  return retval;
70 }
#define PTHREAD_MUTEX_INITIALIZER
static int magmawinthread_initialized
CRITICAL_SECTION magmawinthread_static_initializer_check_lock
MAGMA_DLLPORT int MAGMA_CDECL pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)

Here is the call graph for this function:

Here is the caller graph for this function:

MAGMA_DLLPORT int MAGMA_CDECL pthread_mutex_destroy ( pthread_mutex_t mutex)

Definition at line 117 of file magmawinthread.c.

117  {
118  CloseHandle( *mutex );
119  return 0;
120 }

Here is the caller graph for this function:

MAGMA_DLLPORT int MAGMA_CDECL pthread_mutex_init ( pthread_mutex_t mutex,
const pthread_mutexattr_t attr 
)

no security atributes

not owned (initialy) by the creating thread

no name provided: cannot be shared between processes

Definition at line 48 of file magmawinthread.c.

48  {
49  *mutex =
50  CreateMutex( NULL,
51  FALSE,
52  NULL
53  );
54 
55  return 0;
56 }
Definition: quark.c:94

Here is the caller graph for this function:

MAGMA_DLLPORT int MAGMA_CDECL pthread_mutex_lock ( pthread_mutex_t mutex)

the wait was succesful

the wait failed

thread killed during the wait

impossible because of INFINITE

Definition at line 72 of file magmawinthread.c.

72  {
73  DWORD rv;
74 
76  rv = WaitForSingleObject( *mutex, INFINITE );
77  switch (rv) {
78  case WAIT_OBJECT_0:
79  return 0;
80  case WAIT_FAILED:
81  return -1;
82  case WAIT_ABANDONED:
83  return -1;
84  case WAIT_TIMEOUT:
85  return -1;
86  default:
87  return -1;
88  }
89 }
static int pthread_mutex_check_for_static_initialization(pthread_mutex_t *mutex)
#define PTHREAD_MUTEX_INITIALIZER

Here is the caller graph for this function:

MAGMA_DLLPORT int MAGMA_CDECL pthread_mutex_trylock ( pthread_mutex_t mutex)

the wait was succesful

the wait failed

thread killed during the wait

impossible because of INFINITE

Definition at line 91 of file magmawinthread.c.

91  {
92  DWORD rv;
93 
95  rv = WaitForSingleObject( *mutex, 0 );
96  switch (rv) {
97  case WAIT_OBJECT_0:
98  return 0;
99  case WAIT_FAILED:
100  return -1;
101  case WAIT_ABANDONED:
102  return -1;
103  case WAIT_TIMEOUT:
104  return -1;
105  default:
106  return -1;
107  }
108 }
static int pthread_mutex_check_for_static_initialization(pthread_mutex_t *mutex)
#define PTHREAD_MUTEX_INITIALIZER

Here is the caller graph for this function:

MAGMA_DLLPORT int MAGMA_CDECL pthread_mutex_unlock ( pthread_mutex_t mutex)

Definition at line 110 of file magmawinthread.c.

110  {
111  if (! ReleaseMutex( *mutex ))
112  return -1;
113 
114  return 0;
115 }

Here is the caller graph for this function:

MAGMA_DLLPORT pthread_t MAGMA_CDECL pthread_self ( void  )

Definition at line 34 of file magmawinthread.c.

34  {
35  pthread_t pt;
36 
37  pt.hThread = GetCurrentThread();
38  pt.uThId = GetCurrentThreadId();
39  return pt;
40 }
HANDLE hThread
unsigned int uThId

Here is the caller graph for this function:

MAGMA_DLLPORT unsigned int MAGMA_CDECL pthread_self_id ( void  )

Definition at line 30 of file magmawinthread.c.

30  {
31  return GetCurrentThreadId();
32 }
MAGMA_DLLPORT int MAGMA_CDECL pthread_setconcurrency ( int  level)

Definition at line 266 of file magmawinthread.c.

266  {
267  pthread_conclevel = level;
268  return 0;
269 }
int pthread_conclevel

Here is the caller graph for this function:

Variable Documentation

void*(* MAGMA_realThStart)(void *)

Definition at line 142 of file magmawinthread.c.

int magmawinthread_initialized = 0
static

Definition at line 28 of file magmawinthread.c.

CRITICAL_SECTION magmawinthread_static_initializer_check_lock

this is needed to get a declaration for _beginthreadex()

Definition at line 27 of file magmawinthread.c.

int pthread_conclevel

Definition at line 264 of file magmawinthread.c.