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