This repository has been archived by the owner on Jun 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpthread.c
1215 lines (1008 loc) · 34.6 KB
/
pthread.c
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
m5threads, a pthread library for the M5 simulator
Copyright (C) 2009, Stanford University
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author: Daniel Sanchez
*/
#include <unistd.h>
#include <assert.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <sys/errno.h>
#include <sched.h>
#include <linux/sched.h>
#include <sys/mman.h>
#include <string.h>
#include <malloc.h>
#include <sys/syscall.h>
//Spinlock assembly
#if defined(__x86) || defined(__x86_64)
#include "spinlock_x86.h"
#elif defined(__alpha)
#include "spinlock_alpha.h"
#elif defined(__sparc)
#include "spinlock_sparc.h"
#elif defined (__arm__)
#include "spinlock_arm.h"
#else
#error "spinlock routines not available for your arch!\n"
#endif
#include "pthread_defs.h"
#include "tls_defs.h"
#include "profiling_hooks.h"
#define restrict
//64KB stack, change to your taste...
#define CHILD_STACK_BITS 16
#define CHILD_STACK_SIZE (1 << CHILD_STACK_BITS)
//Debug macro
#ifdef __DEBUG
#define DEBUG(args...) printf(args)
#else
#define DEBUG(args...)
#endif
//Size and alignment requirements of "real" (NPTL/LinuxThreads) thread control block
#define NPTL_TCB_SIZE 1184 // sizeof (struct pthread)
#define NPTL_TCB_ALIGN sizeof(double)
#define NPTL_TCBHEAD_T_SIZE (sizeof(tcbhead_t))
//Thread control structure
typedef struct {
pthread_t tid;
unsigned int is_detached; //0 if joinable, 1 if detached
volatile int child_finished;
void* result; //written by child on exit
void *(*start_routine)(void*);
void* arg;
//thread block limits
void* tls_start_addr;
void* stack_start_addr;
} pthread_tcb_t;
//Information about the thread block (TLS, sizes)
static struct {
size_t tls_memsz;
size_t tls_filesz;
void* tls_initimage;
size_t tls_align;
size_t total_size;
size_t stack_guard_size;
} thread_block_info;
/* Thread-local data */
//Pointer to our TCB (NULL for main thread)
__thread pthread_tcb_t* __tcb;
// Used for TSD (getspecific, setspecific, etc.)
__thread void** pthread_specifics = NULL; //dynamically allocated, since this is rarely used
__thread uint32_t pthread_specifics_size = 0;
/* Initialization, create/exit/join functions */
// Search ELF segments, pull out TLS block info, campute thread block sizes
static void populate_thread_block_info() {
ElfW(Phdr) *phdr;
//If there is no TLS segment...
thread_block_info.tls_memsz = 0;
thread_block_info.tls_filesz = 0;
thread_block_info.tls_initimage = NULL;
thread_block_info.tls_align = 0;
/* Look through the TLS segment if there is any. */
if (_dl_phdr != NULL) {
for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr) {
if (phdr->p_type == PT_TLS) {
/* Gather the values we need. */
thread_block_info.tls_memsz = phdr->p_memsz;
thread_block_info.tls_filesz = phdr->p_filesz;
thread_block_info.tls_initimage = (void *) phdr->p_vaddr;
thread_block_info.tls_align = phdr->p_align;
break;
}
}
}
//Set a stack guard size
//In SPARC, this is actually needed to avoid out-of-range accesses on register saves...
//Largest I have seen is 2048 (sparc64)
//You could avoid this in theory by compiling with -mnostack-bias
thread_block_info.stack_guard_size = 2048;
//Total thread block size -- this is what we'll request to mmap
#if TLS_TCB_AT_TP
size_t sz = sizeof(pthread_tcb_t) + thread_block_info.tls_memsz + NPTL_TCBHEAD_T_SIZE + thread_block_info.stack_guard_size + CHILD_STACK_SIZE;
#elif TLS_DTV_AT_TP
size_t sz = sizeof(pthread_tcb_t) + thread_block_info.tls_memsz + NPTL_TCB_SIZE + NPTL_TCBHEAD_T_SIZE + thread_block_info.stack_guard_size + CHILD_STACK_SIZE;
#else
#error "TLS_TCB_AT_TP xor TLS_DTV_AT_TP must be defined"
#endif
//Note that TCB_SIZE is the "real" TCB size, not ours, which we leave zeroed (but some variables, notably errno, are somewhere inside there)
//Align to multiple of CHILD_STACK_SIZE
sz += CHILD_STACK_SIZE - 1;
thread_block_info.total_size = (sz>>CHILD_STACK_BITS)<<CHILD_STACK_BITS;
}
//Set up TLS block in current thread
// @param th_block_addr: beginning of entire thread memory space
static void setup_thread_tls(void* th_block_addr) {
size_t tcb_offset = 0;
void *tlsblock = NULL;
char *tls_start_ptr = NULL;
#if TLS_DTV_AT_TP
th_block_addr += NPTL_TCB_SIZE;
#endif
/* Compute the (real) TCB offset */
#if TLS_DTV_AT_TP
tcb_offset = roundup(NPTL_TCBHEAD_T_SIZE, NPTL_TCB_ALIGN);
#elif TLS_TCB_AT_TP
tcb_offset = roundup(thread_block_info.tls_memsz, NPTL_TCB_ALIGN);
#else
#error "TLS_TCB_AT_TP xor TLS_DTV_AT_TP must be defined"
#endif
/* Align the TLS block. */
tlsblock = (void *) (((uintptr_t) th_block_addr + thread_block_info.tls_align - 1)
& ~(thread_block_info.tls_align - 1));
/* Initialize the TLS block. */
#if TLS_DTV_AT_TP
tls_start_ptr = ((char *) tlsblock + tcb_offset);
#elif TLS_TCB_AT_TP
tls_start_ptr = ((char *) tlsblock + tcb_offset
- roundup (thread_block_info.tls_memsz, thread_block_info.tls_align ?: 1));
#else
#error "TLS_TCB_AT_TP xor TLS_DTV_AT_TP must be defined"
#endif
//DEBUG("Init TLS: Copying %d bytes from 0x%llx to 0x%llx\n", filesz, (uint64_t) initimage, (uint64_t) tls_start_ptr);
memcpy (tls_start_ptr, thread_block_info.tls_initimage, thread_block_info.tls_filesz);
//Rest of tls vars are already cleared (mmap returns zeroed memory)
//Note: We don't care about DTV pointers for x86/SPARC -- they're never used in static mode
/* Initialize the thread pointer. */
#if TLS_DTV_AT_TP
TLS_INIT_TP (tlsblock, 0);
#elif TLS_TCB_AT_TP
TLS_INIT_TP ((char *) tlsblock + tcb_offset, 0);
#else
#error "TLS_TCB_AT_TP xor TLS_DTV_AT_TP must be defined"
#endif
}
//Some NPTL definitions
int __libc_multiple_threads; //set to one on initialization
int __nptl_nthreads = 32; //TODO: we don't really know...
//Called at initialization. Sets up TLS for the main thread and populates thread_block_info, used in subsequent calls
//Works with LinuxThreads and NPTL
void __pthread_initialize_minimal() {
__libc_multiple_threads = 1; //tell libc we're multithreaded (NPTL-specific)
populate_thread_block_info();
void* ptr = mmap(0, thread_block_info.total_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
setup_thread_tls(ptr + sizeof(pthread_tcb_t));
}
//Used by pthread_create to spawn child
static int __pthread_trampoline(void* thr_ctrl) {
//Set TLS up
pthread_tcb_t* tcb = (pthread_tcb_t*) thr_ctrl;
setup_thread_tls(tcb->tls_start_addr);
__tcb = tcb;
DEBUG("Child in trampoline, TID=%llx\n", tcb->tid);
void* result = tcb->start_routine(tcb->arg);
pthread_exit(result);
assert(0); //should never be reached
}
int pthread_create (pthread_t* thread,
const pthread_attr_t* attr,
void *(*start_routine)(void*),
void* arg) {
DEBUG("pthread_create: start\n");
//Allocate the child thread block (TCB+TLS+stack area)
//We use mmap so that the child can munmap it at exit without using a stack (it's a system call)
void* thread_block;
size_t thread_block_size = thread_block_info.total_size;
thread_block = mmap(0, thread_block_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
DEBUG("pthread_create: mmapped child thread block 0x%llx -- 0x%llx\n", thread_block, ((char*)thread_block) + CHILD_STACK_SIZE) ;
//Populate the thread control block
pthread_tcb_t* tcb = (pthread_tcb_t*) thread_block;
tcb->tid = (pthread_t) thread_block; //thread ID is tcb address itself
tcb->is_detached = 0; //joinable
tcb->child_finished = 0;
tcb->start_routine = start_routine;
tcb->arg = arg;
tcb->tls_start_addr = (void*)(((char*)thread_block) + sizeof(pthread_tcb_t)); //right after m5's tcb
tcb->stack_start_addr = (void*) (((char*) thread_block) + thread_block_size - thread_block_info.stack_guard_size); //end of thread_block
*thread=(pthread_t) thread_block;
//Call clone()
DEBUG("pthread_create: prior to clone()\n");
clone(__pthread_trampoline, tcb->stack_start_addr, CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD, tcb);
DEBUG("pthread_create: after clone()\n");
return 0;
}
pthread_t pthread_self() {
if (__tcb == NULL) return 0; //main thread
return __tcb->tid;
}
int pthread_join (pthread_t thread, void** status) {
DEBUG("pthread_join: started\n");
pthread_tcb_t* child_tcb = (pthread_tcb_t*) thread;
assert(child_tcb->tid == thread); // checks that this is really a tcb
assert(!child_tcb->is_detached); // thread should be joinable
volatile int child_done = 0;
while (child_done == 0) { // spin until child done
child_done = child_tcb->child_finished;
}
DEBUG("pthread_join: child joined\n");
//Get result
if (status) *status = child_tcb->result;
//Deallocate child block
//munmap(child_tcb, thread_block_info.total_size);
return 0;
}
void pthread_exit (void* status) {
// TODO: The good way to solve this is to have the child, not its parent, free
// its own stack (and TLS segment). This enables detached threads. But to do this
// you need an extra stack. A way to do this is to have a global, lock-protected
// manager stack, or have the M5 exit system call do it... Anyhow, I'm deferring
// this problem until we have TLS.
//From point (XXX) on, the thread **does not exist**,
//as its parent may have already freed the stack.
//So we must call sys_exit without using the stack => asm
// NOTE: You may be tempted to call exit(0) or _exit(0) here, but there call exit_group,
// killing the whole process and not just the current thread
//If the keys array was allocated, free it
if (pthread_specifics != NULL) free(pthread_specifics);
//Main thread
if (__tcb == NULL) _exit(0);
DEBUG("Child TID=0x%llx in pthread_exit...\n", pthread_self() );
__tcb->result = status;
//TODO mem barrier here...
__tcb->child_finished = 1;
//XXX
syscall(__NR_exit,0);
assert(0); //should never be reached
/*#if defined(__x86) or defined(__x86_64)
__asm__ __volatile__ (
"\nmov $0x3c,%%eax\n\t" \
"syscall\n\t"
::: "eax");
#elif defined(__alpha)
__asm__ __volatile__ (
"\nldi $0,1\n\t" \
"callsys\n\t");
#elif defined(__sparc)
// Since this part of the code is provisional, don't bother with asm for now
syscall(__NR_exit,0);
#else
#error "No pthread_exit asm for your arch, sorry!\n"
#endif
assert(0);*/
}
// mutex functions
int pthread_mutex_init (pthread_mutex_t* mutex, const pthread_mutexattr_t* attr) {
DEBUG("%s: start\n", __FUNCTION__);
mutex->PTHREAD_MUTEX_T_COUNT = 0;
return 0;
}
int pthread_mutex_lock (pthread_mutex_t* lock) {
DEBUG("%s: start\n", __FUNCTION__);
PROFILE_LOCK_START(lock);
spin_lock((int*)&lock->PTHREAD_MUTEX_T_COUNT);
PROFILE_LOCK_END(lock);
return 0;
}
int pthread_mutex_unlock (pthread_mutex_t* lock) {
DEBUG("%s: start\n", __FUNCTION__);
PROFILE_UNLOCK_START(lock);
spin_unlock((int*)&lock->PTHREAD_MUTEX_T_COUNT);
PROFILE_UNLOCK_END(lock);
return 0;
}
int pthread_mutex_destroy (pthread_mutex_t* mutex) {
DEBUG("%s: start\n", __FUNCTION__);
return 0;
}
int pthread_mutex_trylock (pthread_mutex_t* mutex) {
DEBUG("%s: start\n", __FUNCTION__);
int acquired = trylock((int*)&mutex->PTHREAD_MUTEX_T_COUNT);
if (acquired == 1) {
//Profiling not really accurate here...
PROFILE_LOCK_START(mutex);
PROFILE_LOCK_END(mutex);
return 0;
}
return EBUSY;
}
// rwlock functions
int pthread_rwlock_init (pthread_rwlock_t* lock, const pthread_rwlockattr_t* attr) {
DEBUG("%s: start\n", __FUNCTION__);
PTHREAD_RWLOCK_T_LOCK(lock) = 0; // used only with spin_lock, so we know to initilize to zero
PTHREAD_RWLOCK_T_READERS(lock) = 0;
PTHREAD_RWLOCK_T_WRITER(lock) = -1; // -1 means no one owns the write lock
return 0;
}
int pthread_rwlock_destroy (pthread_rwlock_t* lock) {
DEBUG("%s: start\n", __FUNCTION__);
return 0;
}
int pthread_rwlock_rdlock (pthread_rwlock_t* lock) {
DEBUG("%s: start\n", __FUNCTION__);
PROFILE_LOCK_START(lock);
do {
// this is to reduce the contention and a possible live-lock to lock->access_lock
while (1) {
pthread_t writer = PTHREAD_RWLOCK_T_WRITER(lock);
if (writer == -1) {
break;
}
}
spin_lock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
if ((pthread_t)PTHREAD_RWLOCK_T_WRITER(lock) == -1) {
PTHREAD_RWLOCK_T_READERS(lock)++;
spin_unlock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
PROFILE_LOCK_END(lock);
return 0;
}
spin_unlock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
} while (1);
PROFILE_LOCK_END(lock);
return 0;
}
int pthread_rwlock_wrlock (pthread_rwlock_t* lock) {
DEBUG("%s: start\n", __FUNCTION__);
PROFILE_LOCK_START(lock);
do {
while (1) {
pthread_t writer = PTHREAD_RWLOCK_T_WRITER(lock);
if (writer == -1) {
break;
}
int num_readers = PTHREAD_RWLOCK_T_READERS(lock);
if (num_readers == 0) {
break;
}
}
spin_lock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
if ((pthread_t)PTHREAD_RWLOCK_T_WRITER(lock) == -1 && PTHREAD_RWLOCK_T_READERS(lock) == 0) {
PTHREAD_RWLOCK_T_WRITER(lock) = pthread_self();
spin_unlock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
PROFILE_LOCK_END(lock);
return 0;
}
spin_unlock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
} while (1);
PROFILE_LOCK_END(lock);
return 0;
}
int pthread_rwlock_unlock (pthread_rwlock_t* lock) {
DEBUG("%s: start\n", __FUNCTION__);
PROFILE_UNLOCK_START(lock);
spin_lock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
if (pthread_self() == PTHREAD_RWLOCK_T_WRITER(lock)) {
// the write lock will be released
PTHREAD_RWLOCK_T_WRITER(lock) = -1;
} else {
// one of the read locks will be released
PTHREAD_RWLOCK_T_READERS(lock) = PTHREAD_RWLOCK_T_READERS(lock) - 1;
}
spin_unlock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock)));
PROFILE_UNLOCK_END(lock);
return 0;
}
// key functions
#ifndef PTHREAD_KEYS_MAX
#define PTHREAD_KEYS_MAX 1024
#endif
typedef struct {
int in_use;
void (*destr)(void*);
} pthread_key_struct;
static pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX];
static pthread_mutex_t pthread_keys_mutex = PTHREAD_MUTEX_INITIALIZER;
int pthread_key_create (pthread_key_t* key, void (*destructor)(void*)) {
int i;
DEBUG("%s: start\n", __FUNCTION__);
pthread_mutex_lock(&pthread_keys_mutex);
for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
if (! pthread_keys[i].in_use) {
/* Mark key in use */
pthread_keys[i].in_use = 1;
pthread_keys[i].destr = destructor;
pthread_mutex_unlock(&pthread_keys_mutex);
*key = i;
return 0;
}
}
pthread_mutex_unlock(&pthread_keys_mutex);
return EAGAIN;
}
int pthread_key_delete (pthread_key_t key)
{
DEBUG("%s: start\n", __FUNCTION__);
pthread_mutex_lock(&pthread_keys_mutex);
if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use) {
pthread_mutex_unlock(&pthread_keys_mutex);
return EINVAL;
}
pthread_keys[key].in_use = 0;
pthread_keys[key].destr = NULL;
/* NOTE: The LinuxThreads implementation actually zeroes deleted keys on
spawned threads. I don't care, the spec says that if you are access a
key after if has been deleted, you're on your own. */
pthread_mutex_unlock(&pthread_keys_mutex);
return 0;
}
int pthread_setspecific (pthread_key_t key, const void* value) {
int m_size;
DEBUG("%s: start\n", __FUNCTION__);
if (key < 0 || key >= PTHREAD_KEYS_MAX) return EINVAL;
if (pthread_specifics_size == 0) {
pthread_specifics = (void**) calloc(PTHREAD_KEYS_MAX + 1, sizeof(void*));
DEBUG("pthread_setspecific: malloc of size %d bytes, got 0x%llx\n", m_size, pthread_specifics);
pthread_specifics_size = key+1;
}
pthread_specifics[key] = (void*) value;
return 0;
}
void* pthread_getspecific (pthread_key_t key) {
if (key < 0 || key >= pthread_specifics_size) return NULL;
DEBUG("pthread_getspecific: key=%d pthread_specifics_size=%d\n", key, pthread_specifics_size);
return pthread_specifics[key];
}
// condition variable functions
int pthread_cond_init (pthread_cond_t* cond, const pthread_condattr_t* attr) {
DEBUG("%s: start\n", __FUNCTION__);
PTHREAD_COND_T_FLAG(cond) = 0;
PTHREAD_COND_T_THREAD_COUNT(cond) = 0;
PTHREAD_COND_T_COUNT_LOCK(cond) = 0;
return 0;
}
int pthread_cond_destroy (pthread_cond_t* cond) {
DEBUG("%s: start\n", __FUNCTION__);
return 0;
}
int pthread_cond_broadcast (pthread_cond_t* cond) {
DEBUG("%s: start\n", __FUNCTION__);
PTHREAD_COND_T_FLAG(cond) = 1;
return 0;
}
int pthread_cond_wait (pthread_cond_t* cond, pthread_mutex_t* lock) {
DEBUG("%s: start\n", __FUNCTION__);
PROFILE_COND_WAIT_START(cond);
volatile int* thread_count = &(PTHREAD_COND_T_THREAD_COUNT(cond));
volatile int* flag = &(PTHREAD_COND_T_FLAG(cond));
volatile int* count_lock = &(PTHREAD_COND_T_COUNT_LOCK(cond));
// dsm: ++/-- have higher precedence than *, so *thread_count++
// increments *the pointer*, then dereferences it (!)
(*thread_count)++;
pthread_mutex_unlock(lock);
while (1) {
volatile int f = *flag;
if (f == 1) {
break;
}
}
spin_lock(count_lock);
(*thread_count)--;
if (*thread_count == 0) {
*flag = 0;
}
spin_unlock(count_lock);
pthread_mutex_lock(lock);
PROFILE_COND_WAIT_END(cond);
return 0;
}
int pthread_cond_signal (pthread_cond_t* cond) {
DEBUG("%s: start\n", __FUNCTION__);
//Could also signal only one thread, but this is compliant too
//TODO: Just wake one thread up
return pthread_cond_broadcast(cond);
}
//barrier functions
//These funny tree barriers will only work with consecutive TIDs starting from 0, e.g. a barrier initialized for 8 thread will need to be taken by TIDs 0-7
//TODO: Adapt to work with arbitrary TIDs
/*int pthread_barrier_init (pthread_barrier_t *restrict barrier,
const pthread_barrierattr_t *restrict attr, unsigned count)
{
assert(barrier != NULL);
//assert(0 < count && count <= MAX_NUM_CPUS);
PTHREAD_BARRIER_T_NUM_THREADS(barrier) = count;
// add one to avoid false sharing
tree_barrier_t* ptr
= ((tree_barrier_t*)malloc((count + 1) * sizeof(tree_barrier_t))) + 1;
for (unsigned i = 0; i < count; ++i) {
ptr[i].value = 0;
}
PTHREAD_BARRIER_T_BARRIER_PTR(barrier) = ptr;
return 0;
}
int pthread_barrier_destroy (pthread_barrier_t *barrier)
{
free(PTHREAD_BARRIER_T_BARRIER_PTR(barrier) - 1);
return 0;
}
int pthread_barrier_wait (pthread_barrier_t* barrier)
{
int const num_threads = PTHREAD_BARRIER_T_NUM_THREADS(barrier);
int const self = pthread_self();
tree_barrier_t * const barrier_ptr = PTHREAD_BARRIER_T_BARRIER_PTR(barrier);
int const goal = 1 - barrier_ptr[self].value;
int round_mask = 3;
while ((self & round_mask) == 0 && round_mask < (num_threads << 2)) {
int const spacing = (round_mask + 1) >> 2;
for (int i = 1; i <= 3 && self + i*spacing < num_threads; ++i) {
while (barrier_ptr[self + i*spacing].value != goal) {
// spin
}
}
round_mask = (round_mask << 2) + 3;
}
barrier_ptr[self].value = goal;
while (barrier_ptr[0].value != goal) {
// spin
}
return 0;
}*/
int pthread_barrier_init (pthread_barrier_t *restrict barrier,
const pthread_barrierattr_t *restrict attr, unsigned count)
{
assert(barrier != NULL);
DEBUG("%s: start\n", __FUNCTION__);
PTHREAD_BARRIER_T_NUM_THREADS(barrier) = count;
PTHREAD_BARRIER_T_SPINLOCK(barrier) = 0;
PTHREAD_BARRIER_T_COUNTER(barrier) = 0;
PTHREAD_BARRIER_T_DIRECTION(barrier) = 0; //up
return 0;
}
int pthread_barrier_destroy (pthread_barrier_t *barrier)
{
DEBUG("%s: start\n", __FUNCTION__);
//Nothing to do
return 0;
}
int pthread_barrier_wait (pthread_barrier_t* barrier)
{
DEBUG("%s: start\n", __FUNCTION__);
PROFILE_BARRIER_WAIT_START(barrier);
int const initial_direction = PTHREAD_BARRIER_T_DIRECTION(barrier); //0 == up, 1 == down
if (initial_direction == 0) {
spin_lock(&(PTHREAD_BARRIER_T_SPINLOCK(barrier)));
PTHREAD_BARRIER_T_COUNTER(barrier)++;
if (PTHREAD_BARRIER_T_COUNTER(barrier) == PTHREAD_BARRIER_T_NUM_THREADS(barrier)) {
//reverse direction, now down
PTHREAD_BARRIER_T_DIRECTION(barrier) = 1;
}
spin_unlock(&(PTHREAD_BARRIER_T_SPINLOCK(barrier)));
} else {
spin_lock(&(PTHREAD_BARRIER_T_SPINLOCK(barrier)));
PTHREAD_BARRIER_T_COUNTER(barrier)--;
if (PTHREAD_BARRIER_T_COUNTER(barrier) == 0) {
//reverse direction, now up
PTHREAD_BARRIER_T_DIRECTION(barrier) = 0;
}
spin_unlock(&(PTHREAD_BARRIER_T_SPINLOCK(barrier)));
}
volatile int direction = PTHREAD_BARRIER_T_DIRECTION(barrier);
while (initial_direction == direction) {
//spin
direction = PTHREAD_BARRIER_T_DIRECTION(barrier);
}
PROFILE_BARRIER_WAIT_END(barrier);
return 0;
}
//misc functions
static pthread_mutex_t __once_mutex = PTHREAD_MUTEX_INITIALIZER;
int pthread_once (pthread_once_t* once,
void (*init)(void))
{
DEBUG("%s: start\n", __FUNCTION__);
//fast path
if (*once != PTHREAD_ONCE_INIT) return 0;
pthread_mutex_lock(&__once_mutex);
if (*once != PTHREAD_ONCE_INIT) {
pthread_mutex_unlock(&__once_mutex);
return 0;
}
*once = PTHREAD_ONCE_INIT+1;
pthread_mutex_unlock(&__once_mutex);
init();
return 0;
}
#ifndef __USE_EXTERN_INLINES
int pthread_equal (pthread_t t1, pthread_t t2)
{
return t1 == t2; //that was hard :-)
}
#endif
// Functions that we want defined, but we don't use them
// All other functions are not defined so that they will cause a compile time
// error and we can decide if we need to do something with them
// functions really don't need to do anything
int pthread_yield() {
DEBUG("%s: start\n", __FUNCTION__);
// nothing else to yield to
return 0;
}
int pthread_attr_init (pthread_attr_t* attr) {
DEBUG("%s: start\n", __FUNCTION__);
return 0;
}
int pthread_attr_setscope (pthread_attr_t* attr, int scope) {
DEBUG("%s: start\n", __FUNCTION__);
return 0;
}
int pthread_rwlockattr_init (pthread_rwlockattr_t* attr) {
DEBUG("%s: start\n", __FUNCTION__);
return 0;
}
int pthread_attr_setstacksize (pthread_attr_t* attr, size_t stacksize) {
DEBUG("%s: start\n", __FUNCTION__);
return 0;
}
int pthread_attr_setschedpolicy (pthread_attr_t* attr, int policy) {
DEBUG("%s: start\n", __FUNCTION__);
return 0;
}
// some functions that we don't really support
int pthread_setconcurrency (int new_level) {
DEBUG("%s: start\n", __FUNCTION__);
return 0;
}
int pthread_setcancelstate (int p0, int* p1)
{
DEBUG("%s: start\n", __FUNCTION__);
//NPTL uses this
return 0;
}
//and some affinity functions (used by libgomp, openmp)
int pthread_getaffinity_np(pthread_t thread, size_t size, cpu_set_t *set) {
DEBUG("%s: start\n", __FUNCTION__);
char *p = (char*)set;
while ( size-- ) *p++ = 0;
return 0;
}
int pthread_setaffinity_np(pthread_t thread, size_t size, cpu_set_t *set) {
DEBUG("%s: start\n", __FUNCTION__);
return 0;
}
int pthread_attr_setaffinity_np(pthread_attr_t attr, size_t cpusetsize, const cpu_set_t *cpuset) {
DEBUG("%s: start\n", __FUNCTION__);
return 0;
}
int pthread_attr_getaffinity_np(pthread_attr_t attr, size_t cpusetsize, cpu_set_t *cpuset) {
DEBUG("%s: start\n", __FUNCTION__);
return 0;
}
// ... including any dealing with thread-level signal handling
// (maybe we should throw an error message instead?)
int pthread_sigmask (int how, const sigset_t* set, sigset_t* oset) {
DEBUG("%s: start\n", __FUNCTION__);
return 0;
}
int pthread_kill (pthread_t thread, int sig) {
assert(0);
}
// unimplemented pthread functions
int pthread_atfork (void (*f0)(void),
void (*f1)(void),
void (*f2)(void))
{
assert(0);
}
int pthread_attr_destroy (pthread_attr_t* attr)
{
assert(0);
}
int pthread_attr_getdetachstate (const pthread_attr_t* attr,
int* b)
{
assert(0);
}
int pthread_attr_getguardsize (const pthread_attr_t* restrict a,
size_t *restrict b)
{
assert(0);
}
int pthread_attr_getinheritsched (const pthread_attr_t *restrict a,
int *restrict b)
{
assert(0);
}
int pthread_attr_getschedparam (const pthread_attr_t *restrict a,
struct sched_param *restrict b)
{
assert(0);
}
int pthread_attr_getschedpolicy (const pthread_attr_t *restrict a,
int *restrict b)
{
assert(0);
}
int pthread_attr_getscope (const pthread_attr_t *restrict a,
int *restrict b)
{
assert(0);
}
int pthread_attr_getstack (const pthread_attr_t *restrict a,
void* *restrict b,
size_t *restrict c)
{
assert(0);
}
int pthread_attr_getstackaddr (const pthread_attr_t *restrict a,
void* *restrict b)
{
assert(0);
}
int pthread_attr_getstacksize (const pthread_attr_t *restrict a,
size_t *restrict b)
{
assert(0);
}
int pthread_attr_setdetachstate (pthread_attr_t* a,
int b)
{
return 0; //FIXME
}
int pthread_attr_setguardsize (pthread_attr_t* a,
size_t b)
{
assert(0);
}
int pthread_attr_setinheritsched (pthread_attr_t* a,
int b)
{
assert(0);
}
int pthread_attr_setschedparam (pthread_attr_t *restrict a,
const struct sched_param *restrict b)
{
assert(0);
}
int pthread_attr_setstack (pthread_attr_t* a,
void* b,
size_t c)
{
assert(0);
}
int pthread_attr_setstackaddr (pthread_attr_t* a,
void* b)
{
assert(0);
}
int pthread_cancel (pthread_t a)
{
assert(0);
}
void _pthread_cleanup_push (struct _pthread_cleanup_buffer *__buffer,
void (*__routine) (void *),
void *__arg)
{
assert(0);
}
void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *__buffer,
int __execute)
{
assert(0);
}
int pthread_cond_timedwait (pthread_cond_t *restrict a,
pthread_mutex_t *restrict b,
const struct timespec *restrict c)
{
assert(0);
}
int pthread_condattr_destroy (pthread_condattr_t* a)
{
assert(0);
}
int pthread_condattr_getpshared (const pthread_condattr_t *restrict a,
int *restrict b)
{
assert(0);
}
int pthread_condattr_init (pthread_condattr_t* a)
{
assert(0);
}
int pthread_condattr_setpshared (pthread_condattr_t* a,
int b)
{
assert(0);
}
int pthread_detach (pthread_t a)
{
assert(0);
}
int pthread_getconcurrency ()
{
assert(0);
}
int pthread_getschedparam(pthread_t a,
int *restrict b,
struct sched_param *restrict c)
{
assert(0);
}
int pthread_mutex_getprioceiling (const pthread_mutex_t *restrict a,
int *restrict b)
{
assert(0);
}
int pthread_mutex_setprioceiling (pthread_mutex_t *restrict a,
int b,
int *restrict c)
{
assert(0);
}