1 //===-- blocked.m --------------------------------------------------*- ObjC -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
18 #include <Foundation/Foundation.h>
21 static pthread_mutex_t test_mutex;
23 static void Mutex_Init (void)
25 pthread_mutexattr_t tmp_mutex_attr;
26 pthread_mutexattr_init(&tmp_mutex_attr);
27 pthread_mutex_init(&test_mutex, &tmp_mutex_attr);
30 @interface LockMe :NSObject
34 - (NSString *) description;
37 @implementation LockMe
38 - (NSString *) description
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";
49 my_pthread_routine (void *data)
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);
61 pthread_attr_t tmp_attr;
62 pthread_attr_init (&tmp_attr);
67 LockMe *lock_me = [[LockMe alloc] init];
68 pthread_create (&my_pthread, &tmp_attr, my_pthread_routine, NULL);
70 pthread_join (my_pthread, NULL);