Home | History | Annotate | Download | only in print-obj
      1 //===-- blocked.m --------------------------------------------------*- ObjC -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // This file is for testing running "print object" in a case where another thread
     11 // blocks the print object from making progress.  Set a breakpoint on the line in 
     12 // my_pthread_routine as indicated.  Then switch threads to the main thread, and
     13 // do print the lock_me object.  Since that will try to get the lock already gotten
     14 // by my_pthread_routime thread, it will have to switch to running all threads, and
     15 // that should then succeed.
     16 //
     17 
     18 #include <Foundation/Foundation.h>
     19 #include <pthread.h>
     20 
     21 static pthread_mutex_t test_mutex;
     22 
     23 static void Mutex_Init (void)
     24 {
     25     pthread_mutexattr_t tmp_mutex_attr;
     26     pthread_mutexattr_init(&tmp_mutex_attr);
     27     pthread_mutex_init(&test_mutex, &tmp_mutex_attr);
     28 }
     29 
     30 @interface LockMe :NSObject
     31 {
     32   
     33 }
     34 - (NSString *) description;
     35 @end
     36 
     37 @implementation LockMe 
     38 - (NSString *) description
     39 {
     40     printf ("LockMe trying to get the lock.\n");
     41     pthread_mutex_lock(&test_mutex);
     42     printf ("LockMe got the lock.\n");
     43     pthread_mutex_unlock(&test_mutex);
     44     return @"I am pretty special.\n";
     45 }
     46 @end
     47 
     48 void *
     49 my_pthread_routine (void *data)
     50 {
     51     printf ("my_pthread_routine about to enter.\n");  
     52     pthread_mutex_lock(&test_mutex);
     53     printf ("Releasing Lock.\n"); // Set a breakpoint here.
     54     pthread_mutex_unlock(&test_mutex);
     55     return NULL;
     56 }
     57 
     58 int
     59 main ()
     60 {
     61   pthread_attr_t tmp_attr;
     62   pthread_attr_init (&tmp_attr);
     63   pthread_t my_pthread;
     64 
     65   Mutex_Init ();
     66 
     67   LockMe *lock_me = [[LockMe alloc] init];
     68   pthread_create (&my_pthread, &tmp_attr, my_pthread_routine, NULL);
     69 
     70   pthread_join (my_pthread, NULL);
     71 
     72   return 0;
     73 }
     74