summaryrefslogtreecommitdiffstats
path: root/nsprpub/pr/src/bthreads/btthread.c
blob: c2a1cd82b46529e57a87f6f5b9bd487d4b1571b9 (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <kernel/OS.h>
#include <support/TLS.h>

#include "prlog.h"
#include "primpl.h"
#include "prcvar.h"
#include "prpdce.h"

#include <stdlib.h>
#include <string.h>
#include <signal.h>

/* values for PRThread.state */
#define BT_THREAD_PRIMORD   0x01    /* this is the primordial thread */
#define BT_THREAD_SYSTEM    0x02    /* this is a system thread */
#define BT_THREAD_JOINABLE  0x04	/* this is a joinable thread */

struct _BT_Bookeeping
{
    PRLock *ml;                 /* a lock to protect ourselves */
	sem_id cleanUpSem;		/* the primoridal thread will block on this
							   sem while waiting for the user threads */
    PRInt32 threadCount;	/* user thred count */

} bt_book = { NULL, B_ERROR, 0 };


#define BT_TPD_LIMIT 128	/* number of TPD slots we'll provide (arbitrary) */

/* these will be used to map an index returned by PR_NewThreadPrivateIndex()
   to the corresponding beos native TLS slot number, and to the destructor
   for that slot - note that, because it is allocated globally, this data
   will be automatically zeroed for us when the program begins */
static int32 tpd_beosTLSSlots[BT_TPD_LIMIT];
static PRThreadPrivateDTOR tpd_dtors[BT_TPD_LIMIT];

static vint32 tpd_slotsUsed=0;	/* number of currently-allocated TPD slots */
static int32 tls_prThreadSlot;	/* TLS slot in which PRThread will be stored */

/* this mutex will be used to synchronize access to every
   PRThread.md.joinSem and PRThread.md.is_joining (we could
   actually allocate one per thread, but that seems a bit excessive,
   especially considering that there will probably be little
   contention, PR_JoinThread() is allowed to block anyway, and the code
   protected by the mutex is short/fast) */
static PRLock *joinSemLock;

static PRUint32 _bt_MapNSPRToNativePriority( PRThreadPriority priority );
static PRThreadPriority _bt_MapNativeToNSPRPriority( PRUint32 priority );
static void _bt_CleanupThread(void *arg);
static PRThread *_bt_AttachThread();

void
_PR_InitThreads (PRThreadType type, PRThreadPriority priority,
                 PRUintn maxPTDs)
{
    PRThread *primordialThread;
    PRUint32  beThreadPriority;

	/* allocate joinSem mutex */
	joinSemLock = PR_NewLock();
	if (joinSemLock == NULL)
	{
		PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
		return;
    }

    /*
    ** Create and initialize NSPR structure for our primordial thread.
    */
    
    primordialThread = PR_NEWZAP(PRThread);
    if( NULL == primordialThread )
    {
        PR_SetError( PR_OUT_OF_MEMORY_ERROR, 0 );
        return;
    }

	primordialThread->md.joinSem = B_ERROR;

    /*
    ** Set the priority to the desired level.
    */

    beThreadPriority = _bt_MapNSPRToNativePriority( priority );
    
    set_thread_priority( find_thread( NULL ), beThreadPriority );
    
    primordialThread->priority = priority;


	/* set the thread's state - note that the thread is not joinable */
    primordialThread->state |= BT_THREAD_PRIMORD;
	if (type == PR_SYSTEM_THREAD)
		primordialThread->state |= BT_THREAD_SYSTEM;

    /*
    ** Allocate a TLS slot for the PRThread structure (just using
    ** native TLS, as opposed to NSPR TPD, will make PR_GetCurrentThread()
    ** somewhat faster, and will leave one more TPD slot for our client)
    */
	
	tls_prThreadSlot = tls_allocate();

    /*
    ** Stuff our new PRThread structure into our thread specific
    ** slot.
    */

	tls_set(tls_prThreadSlot, primordialThread);
    
	/* allocate lock for bt_book */
    bt_book.ml = PR_NewLock();
    if( NULL == bt_book.ml )
    {
    	PR_SetError( PR_OUT_OF_MEMORY_ERROR, 0 );
	return;
    }
}

PRUint32
_bt_MapNSPRToNativePriority( PRThreadPriority priority )
    {
    switch( priority )
    {
    	case PR_PRIORITY_LOW:	 return( B_LOW_PRIORITY );
	case PR_PRIORITY_NORMAL: return( B_NORMAL_PRIORITY );
	case PR_PRIORITY_HIGH:	 return( B_DISPLAY_PRIORITY );
	case PR_PRIORITY_URGENT: return( B_URGENT_DISPLAY_PRIORITY );
	default:		 return( B_NORMAL_PRIORITY );
    }
}

PRThreadPriority
_bt_MapNativeToNSPRPriority(PRUint32 priority)
    {
	if (priority < B_NORMAL_PRIORITY)
		return PR_PRIORITY_LOW;
	if (priority < B_DISPLAY_PRIORITY)
		return PR_PRIORITY_NORMAL;
	if (priority < B_URGENT_DISPLAY_PRIORITY)
		return PR_PRIORITY_HIGH;
	return PR_PRIORITY_URGENT;
}

PRUint32
_bt_mapNativeToNSPRPriority( int32 priority )
{
    switch( priority )
    {
    	case PR_PRIORITY_LOW:	 return( B_LOW_PRIORITY );
	case PR_PRIORITY_NORMAL: return( B_NORMAL_PRIORITY );
	case PR_PRIORITY_HIGH:	 return( B_DISPLAY_PRIORITY );
	case PR_PRIORITY_URGENT: return( B_URGENT_DISPLAY_PRIORITY );
	default:		 return( B_NORMAL_PRIORITY );
    }
}

/* This method is called by all NSPR threads as they exit */
void _bt_CleanupThread(void *arg)
{
	PRThread *me = PR_GetCurrentThread();
	int32 i;

	/* first, clean up all thread-private data */
	for (i = 0; i < tpd_slotsUsed; i++)
	{
		void *oldValue = tls_get(tpd_beosTLSSlots[i]);
		if ( oldValue != NULL && tpd_dtors[i] != NULL )
			(*tpd_dtors[i])(oldValue);
	}

	/* if this thread is joinable, wait for someone to join it */
	if (me->state & BT_THREAD_JOINABLE)
	{
		/* protect access to our joinSem */
		PR_Lock(joinSemLock);

		if (me->md.is_joining)
		{
			/* someone is already waiting to join us (they've
			   allocated a joinSem for us) - let them know we're
			   ready */
			delete_sem(me->md.joinSem);

			PR_Unlock(joinSemLock);

		}
		else
    {
			/* noone is currently waiting for our demise - it
			   is our responsibility to allocate the joinSem
			   and block on it */
			me->md.joinSem = create_sem(0, "join sem");

			/* we're done accessing our joinSem */
			PR_Unlock(joinSemLock);

			/* wait for someone to join us */
			while (acquire_sem(me->md.joinSem) == B_INTERRUPTED);
	    }
	}

	/* if this is a user thread, we must update our books */
	if ((me->state & BT_THREAD_SYSTEM) == 0)
	{
		/* synchronize access to bt_book */
    PR_Lock( bt_book.ml );

		/* decrement the number of currently-alive user threads */
	bt_book.threadCount--;

		if (bt_book.threadCount == 0 && bt_book.cleanUpSem != B_ERROR) {
			/* we are the last user thread, and the primordial thread is
			   blocked in PR_Cleanup() waiting for us to finish - notify
			   it */
			delete_sem(bt_book.cleanUpSem);
	}

    PR_Unlock( bt_book.ml );
	}

	/* finally, delete this thread's PRThread */
	PR_DELETE(me);
}

/**
 * This is a wrapper that all threads invoke that allows us to set some
 * things up prior to a thread's invocation and clean up after a thread has
 * exited.
 */
static void*
_bt_root (void* arg)
	{
    PRThread *thred = (PRThread*)arg;
    PRIntn rv;
    void *privData;
    status_t result;
    int i;

	/* save our PRThread object into our TLS */
	tls_set(tls_prThreadSlot, thred);

    thred->startFunc(thred->arg);  /* run the dang thing */

	/* clean up */
	_bt_CleanupThread(NULL);

	return 0;
}

PR_IMPLEMENT(PRThread*)
    PR_CreateThread (PRThreadType type, void (*start)(void* arg), void* arg,
                     PRThreadPriority priority, PRThreadScope scope,
                     PRThreadState state, PRUint32 stackSize)
{
    PRUint32 bePriority;

    PRThread* thred;

    if (!_pr_initialized) _PR_ImplicitInitialization();

	thred = PR_NEWZAP(PRThread);
 	if (thred == NULL)
	{
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
        return NULL;
    }

    thred->md.joinSem = B_ERROR;

        thred->arg = arg;
        thred->startFunc = start;
        thred->priority = priority;

	if( state == PR_JOINABLE_THREAD )
	{
	    thred->state |= BT_THREAD_JOINABLE;
	}

        /* keep some books */

	PR_Lock( bt_book.ml );

	if (type == PR_USER_THREAD)
	{
	    bt_book.threadCount++;
        }

	PR_Unlock( bt_book.ml );

	bePriority = _bt_MapNSPRToNativePriority( priority );

        thred->md.tid = spawn_thread((thread_func)_bt_root, "moz-thread",
                                     bePriority, thred);
        if (thred->md.tid < B_OK) {
            PR_SetError(PR_UNKNOWN_ERROR, thred->md.tid);
            PR_DELETE(thred);
			return NULL;
        }

        if (resume_thread(thred->md.tid) < B_OK) {
            PR_SetError(PR_UNKNOWN_ERROR, 0);
            PR_DELETE(thred);
			return NULL;
        }

    return thred;
    }

PR_IMPLEMENT(PRThread*)
	PR_AttachThread(PRThreadType type, PRThreadPriority priority,
					PRThreadStack *stack)
{
	/* PR_GetCurrentThread() will attach a thread if necessary */
	return PR_GetCurrentThread();
}

PR_IMPLEMENT(void)
	PR_DetachThread()
{
	/* we don't support detaching */
}

PR_IMPLEMENT(PRStatus)
    PR_JoinThread (PRThread* thred)
{
    status_t eval, status;

    PR_ASSERT(thred != NULL);

	if ((thred->state & BT_THREAD_JOINABLE) == 0)
    {
	PR_SetError( PR_INVALID_ARGUMENT_ERROR, 0 );
	return( PR_FAILURE );
    }

	/* synchronize access to the thread's joinSem */
	PR_Lock(joinSemLock);
	
	if (thred->md.is_joining)
	{
		/* another thread is already waiting to join the specified
		   thread - we must fail */
		PR_Unlock(joinSemLock);
		return PR_FAILURE;
	}

	/* let others know we are waiting to join */
	thred->md.is_joining = PR_TRUE;

	if (thred->md.joinSem == B_ERROR)
	{
		/* the thread hasn't finished yet - it is our responsibility to
		   allocate a joinSem and wait on it */
		thred->md.joinSem = create_sem(0, "join sem");

		/* we're done changing the joinSem now */
		PR_Unlock(joinSemLock);

		/* wait for the thread to finish */
		while (acquire_sem(thred->md.joinSem) == B_INTERRUPTED);

	}
	else
	{
		/* the thread has already finished, and has allocated the
		   joinSem itself - let it know it can finally die */
		delete_sem(thred->md.joinSem);
		
		PR_Unlock(joinSemLock);
    }

	/* make sure the thread is dead */
    wait_for_thread(thred->md.tid, &eval);

    return PR_SUCCESS;
}

PR_IMPLEMENT(PRThread*)
    PR_GetCurrentThread ()
{
    PRThread* thred;

    if (!_pr_initialized) _PR_ImplicitInitialization();

    thred = (PRThread *)tls_get( tls_prThreadSlot);
	if (thred == NULL)
	{
		/* this thread doesn't have a PRThread structure (it must be
		   a native thread not created by the NSPR) - assimilate it */
		thred = _bt_AttachThread();
	}
    PR_ASSERT(NULL != thred);

    return thred;
}

PR_IMPLEMENT(PRThreadScope)
    PR_GetThreadScope (const PRThread* thred)
{
    PR_ASSERT(thred != NULL);
    return PR_GLOBAL_THREAD;
}

PR_IMPLEMENT(PRThreadType)
    PR_GetThreadType (const PRThread* thred)
{
    PR_ASSERT(thred != NULL);
    return (thred->state & BT_THREAD_SYSTEM) ?
        PR_SYSTEM_THREAD : PR_USER_THREAD;
}

PR_IMPLEMENT(PRThreadState)
    PR_GetThreadState (const PRThread* thred)
{
    PR_ASSERT(thred != NULL);
    return (thred->state & BT_THREAD_JOINABLE)?
    					PR_JOINABLE_THREAD: PR_UNJOINABLE_THREAD;
}

PR_IMPLEMENT(PRThreadPriority)
    PR_GetThreadPriority (const PRThread* thred)
{
    PR_ASSERT(thred != NULL);
    return thred->priority;
}  /* PR_GetThreadPriority */

PR_IMPLEMENT(void) PR_SetThreadPriority(PRThread *thred,
                                        PRThreadPriority newPri)
{
    PRUint32 bePriority;

    PR_ASSERT( thred != NULL );

    thred->priority = newPri;
    bePriority = _bt_MapNSPRToNativePriority( newPri );
    set_thread_priority( thred->md.tid, bePriority );
}

PR_IMPLEMENT(PRStatus)
    PR_NewThreadPrivateIndex (PRUintn* newIndex,
                              PRThreadPrivateDTOR destructor)
{
	int32    index;

    if (!_pr_initialized) _PR_ImplicitInitialization();

	/* reserve the next available tpd slot */
	index = atomic_add( &tpd_slotsUsed, 1 );
	if (index >= BT_TPD_LIMIT)
	{
		/* no slots left - decrement value, then fail */
		atomic_add( &tpd_slotsUsed, -1 );
		PR_SetError( PR_TPD_RANGE_ERROR, 0 );
	    return( PR_FAILURE );
	}

	/* allocate a beos-native TLS slot for this index (the new slot
	   automatically contains NULL) */
	tpd_beosTLSSlots[index] = tls_allocate();

	/* remember the destructor */
	tpd_dtors[index] = destructor;

    *newIndex = (PRUintn)index;

    return( PR_SUCCESS );
}

PR_IMPLEMENT(PRStatus)
    PR_SetThreadPrivate (PRUintn index, void* priv)
{
	void *oldValue;

    /*
    ** Sanity checking
    */

    if(index < 0 || index >= tpd_slotsUsed || index >= BT_TPD_LIMIT)
    {
		PR_SetError( PR_TPD_RANGE_ERROR, 0 );
	return( PR_FAILURE );
    }

	/* if the old value isn't NULL, and the dtor for this slot isn't
	   NULL, we must destroy the data */
	oldValue = tls_get(tpd_beosTLSSlots[index]);
	if (oldValue != NULL && tpd_dtors[index] != NULL)
		(*tpd_dtors[index])(oldValue);

	/* save new value */
	tls_set(tpd_beosTLSSlots[index], priv);

	    return( PR_SUCCESS );
	}

PR_IMPLEMENT(void*)
    PR_GetThreadPrivate (PRUintn index)
{
	/* make sure the index is valid */
	if (index < 0 || index >= tpd_slotsUsed || index >= BT_TPD_LIMIT)
    {   
		PR_SetError( PR_TPD_RANGE_ERROR, 0 );
		return NULL;
    }

	/* return the value */
	return tls_get( tpd_beosTLSSlots[index] );
	}


PR_IMPLEMENT(PRStatus)
    PR_Interrupt (PRThread* thred)
{
    PRIntn rv;

    PR_ASSERT(thred != NULL);

    /*
    ** there seems to be a bug in beos R5 in which calling
    ** resume_thread() on a blocked thread returns B_OK instead
    ** of B_BAD_THREAD_STATE (beos bug #20000422-19095).  as such,
    ** to interrupt a thread, we will simply suspend then resume it
    ** (no longer call resume_thread(), check for B_BAD_THREAD_STATE,
    ** the suspend/resume to wake up a blocked thread).  this wakes
    ** up blocked threads properly, and doesn't hurt unblocked threads
    ** (they simply get stopped then re-started immediately)
    */

    rv = suspend_thread( thred->md.tid );
    if( rv != B_NO_ERROR )
    {
        /* this doesn't appear to be a valid thread_id */
        PR_SetError( PR_UNKNOWN_ERROR, rv );
        return PR_FAILURE;
    }

    rv = resume_thread( thred->md.tid );
    if( rv != B_NO_ERROR )
    {
        PR_SetError( PR_UNKNOWN_ERROR, rv );
        return PR_FAILURE;
    }

    return PR_SUCCESS;
}

PR_IMPLEMENT(void)
    PR_ClearInterrupt ()
{
}

PR_IMPLEMENT(PRStatus)
    PR_Yield ()
{
    /* we just sleep for long enough to cause a reschedule (100
       microseconds) */
    snooze(100);
}

#define BT_MILLION 1000000UL

PR_IMPLEMENT(PRStatus)
    PR_Sleep (PRIntervalTime ticks)
{
    bigtime_t tps;
    status_t status;

    if (!_pr_initialized) _PR_ImplicitInitialization();

    tps = PR_IntervalToMicroseconds( ticks );

    status = snooze(tps);
    if (status == B_NO_ERROR) return PR_SUCCESS;

    PR_SetError(PR_NOT_IMPLEMENTED_ERROR, status);
    return PR_FAILURE;
}

PR_IMPLEMENT(PRStatus)
    PR_Cleanup ()
{
    PRThread *me = PR_GetCurrentThread();

    PR_ASSERT(me->state & BT_THREAD_PRIMORD);
    if ((me->state & BT_THREAD_PRIMORD) == 0) {
        return PR_FAILURE;
    }

    PR_Lock( bt_book.ml );

	if (bt_book.threadCount != 0)
    {
		/* we'll have to wait for some threads to finish - create a
		   sem to block on */
		bt_book.cleanUpSem = create_sem(0, "cleanup sem");
    }

    PR_Unlock( bt_book.ml );

	/* note that, if all the user threads were already dead, we
	   wouldn't have created a sem above, so this acquire_sem()
	   will fail immediately */
	while (acquire_sem(bt_book.cleanUpSem) == B_INTERRUPTED);

    return PR_SUCCESS;
}

PR_IMPLEMENT(void)
    PR_ProcessExit (PRIntn status)
{
    exit(status);
}

PRThread *_bt_AttachThread()
{
	PRThread *thread;
	thread_info tInfo;

	/* make sure this thread doesn't already have a PRThread structure */
	PR_ASSERT(tls_get(tls_prThreadSlot) == NULL);

	/* allocate a PRThread structure for this thread */
	thread = PR_NEWZAP(PRThread);
	if (thread == NULL)
	{
		PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
		return NULL;
	}

	/* get the native thread's current state */
	get_thread_info(find_thread(NULL), &tInfo);

	/* initialize new PRThread */
	thread->md.tid = tInfo.thread;
	thread->md.joinSem = B_ERROR;
	thread->priority = _bt_MapNativeToNSPRPriority(tInfo.priority);

	/* attached threads are always non-joinable user threads */
	thread->state = 0;

	/* increment user thread count */
	PR_Lock(bt_book.ml);
	bt_book.threadCount++;
	PR_Unlock(bt_book.ml);

	/* store this thread's PRThread */
	tls_set(tls_prThreadSlot, thread);
	
	/* the thread must call _bt_CleanupThread() before it dies, in order
	   to clean up its PRThread, synchronize with the primordial thread,
	   etc. */
	on_exit_thread(_bt_CleanupThread, NULL);
	
	return thread;
}