File: bli_pthread.c

package info (click to toggle)
python-cython-blis 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 43,676 kB
  • sloc: ansic: 645,510; sh: 2,354; asm: 1,466; python: 821; cpp: 585; makefile: 14
file content (695 lines) | stat: -rw-r--r-- 15,102 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/*

   BLIS
   An object-based framework for developing high-performance BLAS-like
   libraries.

   Copyright (C) 2018, Southern Methodist University
   Copyright (C) 2018, The University of Texas at Austin
   Copyright (C) 2018 - 2019, Advanced Micro Devices, Inc.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are
   met:
    - Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    - Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    - Neither the name(s) of the copyright holder(s) nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#include "blis.h"

#if defined(BLIS_DISABLE_SYSTEM)

// This branch defines a pthread-like API, bli_pthread_*(), and implements it
// in terms of "dummy" code that doesn't depend on POSIX threads or any other
// threading mechanism. See issue #454 to see the use case that prompted this
// feature.
// NOTE: THIS CODE DOES NOT IMPLEMENT THREADING AND IS NOT THREAD-SAFE!

// -- pthread_create(), pthread_join() --

int bli_pthread_create
     (
       bli_pthread_t*            thread,
       const bli_pthread_attr_t* attr,
       void*                   (*start_routine)(void*),
       void*                     arg
     )
{
	//return pthread_create( thread, attr, start_routine, arg );
	start_routine( arg );
	return 0;
}

int bli_pthread_join
     (
       bli_pthread_t thread,
       void**        retval
     )
{
	//return pthread_join( thread, retval );
	return 0;
}

// -- pthread_mutex_*() --

int bli_pthread_mutex_init
     (
       bli_pthread_mutex_t*           mutex,
       const bli_pthread_mutexattr_t* attr
     )
{
	//return pthread_mutex_init( mutex, attr );
	return 0;
}

int bli_pthread_mutex_destroy
     (
       bli_pthread_mutex_t* mutex
     )
{
	//return pthread_mutex_destroy( mutex );
	return 0;
}

int bli_pthread_mutex_lock
     (
       bli_pthread_mutex_t* mutex
     )
{
	//return pthread_mutex_lock( mutex );
	return 0;
}

int bli_pthread_mutex_trylock
     (
       bli_pthread_mutex_t* mutex
     )
{
	//return pthread_mutex_trylock( mutex );
	return 0;
}

int bli_pthread_mutex_unlock
     (
       bli_pthread_mutex_t* mutex
     )
{
	//return pthread_mutex_unlock( mutex );
	return 0;
}

// -- pthread_cond_*() --

int bli_pthread_cond_init
     (
       bli_pthread_cond_t*           cond,
       const bli_pthread_condattr_t* attr
     )
{
	//return pthread_cond_init( cond, attr );
	return 0;
}

int bli_pthread_cond_destroy
     (
       bli_pthread_cond_t* cond
     )
{
	//return pthread_cond_destroy( cond );
	return 0;
}

int bli_pthread_cond_wait
     (
       bli_pthread_cond_t*  cond,
       bli_pthread_mutex_t* mutex
     )
{
	//return pthread_cond_wait( cond, mutex );
	return 0;
}

int bli_pthread_cond_broadcast
     (
       bli_pthread_cond_t* cond
     )
{
	//return pthread_cond_broadcast( cond );
	return 0;
}

// -- pthread_once() --

void bli_pthread_once
     (
       bli_pthread_once_t* once,
       void              (*init)(void)
     )
{
	//pthread_once( once, init );
	init();
}

#if 0
// NOTE: This part of the API is disabled because (1) we don't actually need
// _self() or _equal() yet, and (2) when we do try to include these functions,
// AppVeyor for some reason fails on all the Windows/clang builds with the
// error:
//    libblis.a(bli_pthread.o) : error LNK2019: unresolved external symbol
//     __imp_CompareObjectHandles referenced in function bli_pthread_equal

// -- pthread_self() --

bli_pthread_t bli_pthread_self
     (
       void
     )
{
	return 0;
}

// -- pthread_equal() --

int bli_pthread_equal
     (
       bli_pthread_t t1,
       bli_pthread_t t2
     )
{
	// We don't bother comparing t1 and t2 since we must, by definition, be
	// executing the same thread if there is not threading mechanism on the
	// system.
	return 1;
}
#endif

#elif defined(_MSC_VER) // !defined(BLIS_DISABLE_SYSTEM)

#include <errno.h>

// This branch defines a pthread-like API, bli_pthread_*(), and implements it
// in terms of Windows API calls.

// -- pthread_create(), pthread_join() --

typedef struct
{
	void* (*start_routine)( void* );
	void*   param;
	void**  retval;

} bli_thread_param;

static DWORD bli_thread_func
     (
       void* param_
     )
{
	bli_thread_param* param = param_;
	*param->retval = param->start_routine( param->param );
	return 0;
}

int bli_pthread_create
     (
       bli_pthread_t*            thread,
       const bli_pthread_attr_t* attr,
       void*                   (*start_routine)(void*),
       void*                     arg
     )
{
	if ( attr ) return EINVAL;
	bli_thread_param param = { start_routine, arg, &thread->retval };
	thread->handle = CreateThread( NULL, 0, bli_thread_func, &param, 0, NULL );
	if ( !thread->handle ) return EAGAIN;
	return 0;
}

int bli_pthread_join
     (
       bli_pthread_t thread,
       void**        retval
     )
{
	if ( !WaitForSingleObject( thread.handle, INFINITE ) ) return EAGAIN;
	if ( retval ) *retval = thread.retval;
	return 0;
}

// -- pthread_mutex_*() --

int bli_pthread_mutex_init
     (
       bli_pthread_mutex_t*           mutex,
       const bli_pthread_mutexattr_t* attr
     )
{
	if ( attr ) return EINVAL;
	InitializeSRWLock( mutex );
	return 0;
}

int bli_pthread_mutex_destroy
     (
       bli_pthread_mutex_t* mutex
     )
{
	return 0;
}

int bli_pthread_mutex_lock
     (
       bli_pthread_mutex_t* mutex
     )
{
	AcquireSRWLockExclusive( mutex );
	return 0;
}

int bli_pthread_mutex_trylock
     (
       bli_pthread_mutex_t* mutex
     )
{
	return TryAcquireSRWLockExclusive( mutex ) ? 0 : EBUSY;
}

int bli_pthread_mutex_unlock
     (
       bli_pthread_mutex_t* mutex
     )
{
	ReleaseSRWLockExclusive( mutex );
	return 0;
}

// -- pthread_cond_*() --

int bli_pthread_cond_init
     (
       bli_pthread_cond_t*           cond,
       const bli_pthread_condattr_t* attr
     )
{
	if ( attr ) return EINVAL;
	InitializeConditionVariable( cond );
	return 0;
}

int bli_pthread_cond_destroy
     (
       bli_pthread_cond_t* cond
     )
{
	( void )cond;
	return 0;
}

int bli_pthread_cond_wait
     (
       bli_pthread_cond_t*  cond,
       bli_pthread_mutex_t* mutex
     )
{
	if ( !SleepConditionVariableSRW( cond, mutex, INFINITE, 0 ) ) return EAGAIN;
	return 0;
}

int bli_pthread_cond_broadcast
     (
       bli_pthread_cond_t* cond
     )
{
	WakeAllConditionVariable( cond );
	return 0;
}

// -- pthread_once() --

static BOOL bli_init_once_wrapper
     (
       bli_pthread_once_t* once,
       void*               param,
       void**              context
     )
{
	( void )once;
	( void )context;
	typedef void (*callback)( void );
	((callback)param)();
	return TRUE;
}

void bli_pthread_once
     (
       bli_pthread_once_t* once,
       void              (*init)(void)
     )
{
	InitOnceExecuteOnce( once, bli_init_once_wrapper, init, NULL );
}

#if 0
// NOTE: This part of the API is disabled because (1) we don't actually need
// _self() or _equal() yet, and (2) when we do try to include these functions,
// AppVeyor for some reason fails on all the Windows/clang builds with the
// error:
//    libblis.a(bli_pthread.o) : error LNK2019: unresolved external symbol
//     __imp_CompareObjectHandles referenced in function bli_pthread_equal

// -- pthread_self() --

bli_pthread_t bli_pthread_self
     (
       void
     )
{
	bli_pthread_t t;

	// Note: BLIS will only ever use bli_pthread_self() in conjunction with
	// bli_pthread_equal(), and thus setting the .retval field is unnecessary.
	// Despite this, we set it to NULL anyway.
	t.handle = GetCurrentThread();
	t.retval = NULL;

	return t;
}

// -- pthread_equal() --

int bli_pthread_equal
     (
       bli_pthread_t t1,
       bli_pthread_t t2
     )
{
	return ( int )CompareObjectHandles( t1.handle, t2.handle );
}
#endif

#else // !defined(BLIS_DISABLE_SYSTEM) && !defined(_MSC_VER)

// This branch defines a pthreads-like API, bli_pthreads_*(), and implements it
// in terms of the corresponding pthreads_*() types, macros, and function calls. 
// This branch is compiled for Linux and other non-Windows environments where
// we assume that *some* implementation of pthreads is provided (although it
// may lack barriers--see below).

// -- pthread_create(), pthread_join() --

int bli_pthread_create
     (
       bli_pthread_t*            thread,
       const bli_pthread_attr_t* attr,
       void*                   (*start_routine)(void*),
       void*                     arg
     )
{
	return pthread_create( thread, attr, start_routine, arg );
}

int bli_pthread_join
     (
       bli_pthread_t thread,
       void**        retval
     )
{
	return pthread_join( thread, retval );
}

// -- pthread_mutex_*() --

int bli_pthread_mutex_init
     (
       bli_pthread_mutex_t*           mutex,
       const bli_pthread_mutexattr_t* attr
     )
{
	return pthread_mutex_init( mutex, attr );
}

int bli_pthread_mutex_destroy
     (
       bli_pthread_mutex_t* mutex
     )
{
	return pthread_mutex_destroy( mutex );
}

int bli_pthread_mutex_lock
     (
       bli_pthread_mutex_t* mutex
     )
{
	return pthread_mutex_lock( mutex );
}

int bli_pthread_mutex_trylock
     (
       bli_pthread_mutex_t* mutex
     )
{
	return pthread_mutex_trylock( mutex );
}

int bli_pthread_mutex_unlock
     (
       bli_pthread_mutex_t* mutex
     )
{
	return pthread_mutex_unlock( mutex );
}

// -- pthread_cond_*() --

int bli_pthread_cond_init
     (
       bli_pthread_cond_t*           cond,
       const bli_pthread_condattr_t* attr
     )
{
	return pthread_cond_init( cond, attr );
}

int bli_pthread_cond_destroy
     (
       bli_pthread_cond_t* cond
     )
{
	return pthread_cond_destroy( cond );
}

int bli_pthread_cond_wait
     (
       bli_pthread_cond_t*  cond,
       bli_pthread_mutex_t* mutex
     )
{
	return pthread_cond_wait( cond, mutex );
}

int bli_pthread_cond_broadcast
     (
       bli_pthread_cond_t* cond
     )
{
	return pthread_cond_broadcast( cond );
}

// -- pthread_once() --

void bli_pthread_once
     (
       bli_pthread_once_t* once,
       void              (*init)(void)
     )
{
	pthread_once( once, init );
}

#if 0
// NOTE: This part of the API is disabled because (1) we don't actually need
// _self() or _equal() yet, and (2) when we do try to include these functions,
// AppVeyor for some reason fails on all the Windows/clang builds with the
// error:
//    libblis.a(bli_pthread.o) : error LNK2019: unresolved external symbol
//     __imp_CompareObjectHandles referenced in function bli_pthread_equal

// -- pthread_self() --

bli_pthread_t bli_pthread_self
     (
       void
     )
{
	return pthread_self();
}

// -- pthread_equal() --

int bli_pthread_equal
     (
       bli_pthread_t t1,
       bli_pthread_t t2
     )
{
	return pthread_equal( t1, t2 );
}
#endif

#endif // !defined(BLIS_DISABLE_SYSTEM) && !defined(_MSC_VER)




// -- pthread_barrier_*() --

#if defined(BLIS_DISABLE_SYSTEM)

int bli_pthread_barrier_init
     (
       bli_pthread_barrier_t*           barrier,
       const bli_pthread_barrierattr_t* attr,
       unsigned int                     count
     )
{
	//return pthread_barrier_init( barrier, attr, count );
	return 0;
}

int bli_pthread_barrier_destroy
     (
       bli_pthread_barrier_t* barrier
     )
{
	//return pthread_barrier_destroy( barrier );
	return 0;
}

int bli_pthread_barrier_wait
     (
       bli_pthread_barrier_t* barrier
     )
{
	//return pthread_barrier_wait( barrier );
	return 0;
}

#elif defined(__APPLE__) || defined(_MSC_VER) // !defined(BLIS_DISABLE_SYSTEM)

#include <errno.h>

// For OS X and Windows, we define barriers ourselves in terms of the rest
// of the API, though for slightly different reasons: For Windows, we must
// define barriers because we are defining *everything* from scratch. For
// OS X, we must define barriers because Apple chose to omit barriers from
// their implementation of POSIX threads (since barriers are actually
// optional to the POSIX standard).

int bli_pthread_barrier_init
     (
       bli_pthread_barrier_t*           barrier,
       const bli_pthread_barrierattr_t* attr,
       unsigned int                     count )
{
	if ( attr ) return EINVAL;
	if ( count == 0 ) return EINVAL;

	int err;
	if ( (err = bli_pthread_mutex_init( &barrier->mutex, 0 )) != 0 ) return err;
	if ( (err = bli_pthread_cond_init( &barrier->cond, 0 )) != 0 )
	{
		bli_pthread_mutex_destroy( &barrier->mutex );
		return err;
	}
	barrier->tripCount = count;
	barrier->count = 0;

	return 0;
}

int bli_pthread_barrier_destroy
     (
       bli_pthread_barrier_t *barrier
     )
{
	bli_pthread_cond_destroy( &barrier->cond );
	bli_pthread_mutex_destroy( &barrier->mutex );
	return 0;
}

int bli_pthread_barrier_wait
     (
       bli_pthread_barrier_t *barrier
     )
{
	bli_pthread_mutex_lock( &barrier->mutex );
	++(barrier->count);
	if ( barrier->count >= barrier->tripCount )
	{
		barrier->count = 0;
		bli_pthread_cond_broadcast( &barrier->cond );
		bli_pthread_mutex_unlock( &barrier->mutex );
		return 1;
	}
	else
	{
		bli_pthread_cond_wait( &barrier->cond, &(barrier->mutex) );
		bli_pthread_mutex_unlock( &barrier->mutex );
		return 0;
	}
}

#else // !defined(BLIS_DISABLE_SYSTEM) && !defined(__APPLE__) && !defined(_MSC_VER)

// Linux environments implement the pthread_barrier* sub-API. So, if we're
// on Linux, we can simply call those functions, just as we did before for
// the other functions.

int bli_pthread_barrier_init
     (
       bli_pthread_barrier_t*           barrier,
       const bli_pthread_barrierattr_t* attr,
       unsigned int                     count
     )
{
	return pthread_barrier_init( barrier, attr, count );
}

int bli_pthread_barrier_destroy
     (
       bli_pthread_barrier_t* barrier
     )
{
	return pthread_barrier_destroy( barrier );
}

int bli_pthread_barrier_wait
     (
       bli_pthread_barrier_t* barrier
     )
{
	return pthread_barrier_wait( barrier );
}

#endif