~ubuntu-branches/ubuntu/karmic/gnustep-base/karmic

« back to all changes in this revision

Viewing changes to Source/thr-mach.m

  • Committer: Bazaar Package Importer
  • Author(s): Eric Heintzmann
  • Date: 2005-04-17 00:14:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050417001438-enf0y07c9tku85z1
Tags: 1.10.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
#include <mach/mach.h>
30
30
#include <mach/cthreads.h>
31
 
#include <base/objc-gnu2next.h>
32
 
#include <base/thr-mach.h>
 
31
#include "GNUstepBase/objc-gnu2next.h"
 
32
#include "thr-mach.h"
33
33
 
34
34
/* Global exit status. */
35
35
int __objc_thread_exit_status = 0;
41
41
int __objc_runtime_threads_alive = 0;
42
42
 
43
43
/* Thread create/exit mutex */
44
 
struct objc_mutex* __objc_runtime_mutex = NULL; 
 
44
struct objc_mutex* __objc_runtime_mutex = NULL;
45
45
 
46
46
/* The hook function called when the runtime becomes multi threaded */
47
47
objc_thread_callback _objc_became_multi_threaded = NULL;
48
48
 
49
49
/*
50
 
  Use this to set the hook function that will be called when the 
 
50
  Use this to set the hook function that will be called when the
51
51
  runtime initially becomes multi threaded.
52
 
  The hook function is only called once, meaning only when the 
 
52
  The hook function is only called once, meaning only when the
53
53
  2nd thread is spawned, not for each and every thread.
54
54
 
55
55
  It returns the previous hook function or NULL if there is none.
56
56
 
57
57
  A program outside of the runtime could set this to some function so
58
 
  it can be informed; for example, the GNUstep Base Library sets it 
 
58
  it can be informed; for example, the GNUstep Base Library sets it
59
59
  so it can implement the NSBecomingMultiThreaded notification.
60
60
  */
61
61
objc_thread_callback objc_set_thread_callback(objc_thread_callback func)
188
188
  kern_return_t error;
189
189
  struct thread_sched_info info;
190
190
  unsigned int info_count=THREAD_SCHED_INFO_COUNT;
191
 
    
 
191
 
192
192
  if (t == NULL)
193
193
    return -1;
194
194
 
195
195
  threadP  = cthread_thread(t);         /* get thread underlying */
196
196
 
197
 
  error=thread_info(threadP, THREAD_SCHED_INFO, 
 
197
  error=thread_info(threadP, THREAD_SCHED_INFO,
198
198
                    (thread_info_t)&info, &info_count);
199
199
 
200
200
  if (error != KERN_SUCCESS)
234
234
  /* create thread */
235
235
  new_thread_handle = cthread_fork((cthread_fn_t)func, arg);
236
236
 
237
 
  if(new_thread_handle)
 
237
  if (new_thread_handle)
238
238
    {
239
239
      /* this is not terribly portable */
240
 
      thread_id = *(objc_thread_t *)&new_thread_handle; 
 
240
      thread_id = *(objc_thread_t *)&new_thread_handle;
241
241
      cthread_detach(new_thread_handle);
242
242
    }
243
243
  else
244
244
    thread_id = NULL;
245
 
  
 
245
 
246
246
  return thread_id;
247
247
}
248
248
 
251
251
objc_thread_set_priority(int priority)
252
252
{
253
253
  objc_thread_t *t = objc_thread_id();
254
 
  cthread_t cT = (cthread_t) t; 
 
254
  cthread_t cT = (cthread_t) t;
255
255
  int maxPriority = __mach_get_max_thread_priority(cT, NULL);
256
256
  int sys_priority = 0;
257
257
 
297
297
 
298
298
  maxPriority = __mach_get_max_thread_priority(cT, &basePriority);
299
299
 
300
 
  if(maxPriority == -1)
 
300
  if (maxPriority == -1)
301
301
    return -1;
302
302
 
303
 
  if (basePriority > ( (maxPriority * 2) / 3))
 
303
  if (basePriority > ((maxPriority * 2) / 3))
304
304
    return OBJC_THREAD_INTERACTIVE_PRIORITY;
305
305
 
306
 
  if (basePriority > ( maxPriority / 3))
 
306
  if (basePriority > (maxPriority / 3))
307
307
    return OBJC_THREAD_BACKGROUND_PRIORITY;
308
308
 
309
309
  return OBJC_THREAD_LOW_PRIORITY;
444
444
  if (!mutex)
445
445
    return -1;
446
446
 
447
 
  /* If we already own the lock then increment depth */ 
 
447
  /* If we already own the lock then increment depth */
448
448
  thread_id = objc_thread_id();
449
449
  if (mutex->owner == thread_id)
450
450
    return ++mutex->depth;
451
 
    
 
451
 
452
452
  if (mutex_try_lock((mutex_t)(mutex->backend)) == 0)
453
453
    status = -1;
454
454
  else
494
494
/* Backend condition mutex functions */
495
495
 
496
496
/* Allocate a condition. */
497
 
objc_condition_t 
 
497
objc_condition_t
498
498
objc_condition_allocate(void)
499
499
{
500
500
  objc_condition_t condition;
501
 
    
 
501
 
502
502
  /* Allocate the condition mutex structure */
503
 
  if (!(condition = 
 
503
  if (!(condition =
504
504
        (objc_condition_t)objc_malloc(sizeof(struct objc_condition))))
505
505
    return NULL;
506
506
 
591
591
   objc_thread_add() before an alien thread makes any calls to
592
592
   Objective-C.  Do not cause the _objc_became_multi_threaded hook to
593
593
   be executed. */
594
 
void 
 
594
void
595
595
objc_thread_add(void)
596
596
{
597
597
  objc_mutex_lock(__objc_runtime_mutex);
598
598
  __objc_is_multi_threaded = 1;
599
599
  __objc_runtime_threads_alive++;
600
 
  objc_mutex_unlock(__objc_runtime_mutex);  
 
600
  objc_mutex_unlock(__objc_runtime_mutex);
601
601
}
602
602
 
603
603
/* Make the objc thread system aware that a thread managed (started,
610
610
{
611
611
  objc_mutex_lock(__objc_runtime_mutex);
612
612
  __objc_runtime_threads_alive--;
613
 
  objc_mutex_unlock(__objc_runtime_mutex);  
 
613
  objc_mutex_unlock(__objc_runtime_mutex);
614
614
}
615
615
 
616
616
/* End of File */