Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 1 | //===-- examples/ParallelJIT/ParallelJIT.cpp - Exercise threaded-safe JIT -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | fc001bb | 2007-12-29 20:37:57 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Parallel JIT |
| 11 | // |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 12 | // This test program creates two LLVM functions then calls them from three |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 13 | // separate threads. It requires the pthreads library. |
| 14 | // The three threads are created and then block waiting on a condition variable. |
| 15 | // Once all threads are blocked on the conditional variable, the main thread |
| 16 | // wakes them up. This complicated work is performed so that all three threads |
| 17 | // call into the JIT at the same time (or the best possible approximation of the |
| 18 | // same time). This test had assertion errors until I got the locking right. |
| 19 | |
| 20 | #include <pthread.h> |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 21 | #include "llvm/LLVMContext.h" |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/Constants.h" |
Reid Spencer | 5642703 | 2007-01-19 22:45:50 +0000 | [diff] [blame] | 24 | #include "llvm/DerivedTypes.h" |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 25 | #include "llvm/Instructions.h" |
| 26 | #include "llvm/ModuleProvider.h" |
Jeff Cohen | afebb44 | 2006-03-24 03:11:31 +0000 | [diff] [blame] | 27 | #include "llvm/ExecutionEngine/JIT.h" |
| 28 | #include "llvm/ExecutionEngine/Interpreter.h" |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 29 | #include "llvm/ExecutionEngine/GenericValue.h" |
Chris Lattner | da06288 | 2009-06-17 16:48:44 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetSelect.h" |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 31 | #include <iostream> |
| 32 | using namespace llvm; |
| 33 | |
Chris Lattner | 6a98754 | 2007-01-07 07:40:09 +0000 | [diff] [blame] | 34 | static Function* createAdd1(Module *M) { |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 35 | // Create the add1 function entry and insert this entry into module M. The |
| 36 | // function will have a return type of "int" and take an argument of "int". |
| 37 | // The '0' terminates the list of argument types. |
Chris Lattner | 6a98754 | 2007-01-07 07:40:09 +0000 | [diff] [blame] | 38 | Function *Add1F = |
| 39 | cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty, |
| 40 | (Type *)0)); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 41 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 42 | // Add a basic block to the function. As before, it automatically inserts |
| 43 | // because of the last argument. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 44 | BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 45 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 46 | // Get pointers to the constant `1'. |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 47 | Value *One = ConstantInt::get(Type::Int32Ty, 1); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 48 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 49 | // Get pointers to the integer argument of the add1 function... |
| 50 | assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg |
| 51 | Argument *ArgX = Add1F->arg_begin(); // Get the arg |
| 52 | ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 53 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 54 | // Create the add instruction, inserting it into the end of BB. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 55 | Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 56 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 57 | // Create the return instruction and add it to the basic block |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 58 | ReturnInst::Create(Add, BB); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 59 | |
| 60 | // Now, function add1 is ready. |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 61 | return Add1F; |
| 62 | } |
| 63 | |
Chris Lattner | 6a98754 | 2007-01-07 07:40:09 +0000 | [diff] [blame] | 64 | static Function *CreateFibFunction(Module *M) { |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 65 | // Create the fib function and insert it into module M. This function is said |
| 66 | // to return an int and take an int parameter. |
Chris Lattner | 6a98754 | 2007-01-07 07:40:09 +0000 | [diff] [blame] | 67 | Function *FibF = |
| 68 | cast<Function>(M->getOrInsertFunction("fib", Type::Int32Ty, Type::Int32Ty, |
| 69 | (Type *)0)); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 70 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 71 | // Add a basic block to the function. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 72 | BasicBlock *BB = BasicBlock::Create("EntryBlock", FibF); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 73 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 74 | // Get pointers to the constants. |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 75 | Value *One = ConstantInt::get(Type::Int32Ty, 1); |
| 76 | Value *Two = ConstantInt::get(Type::Int32Ty, 2); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 77 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 78 | // Get pointer to the integer argument of the add1 function... |
| 79 | Argument *ArgX = FibF->arg_begin(); // Get the arg. |
| 80 | ArgX->setName("AnArg"); // Give it a nice symbolic name for fun. |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 81 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 82 | // Create the true_block. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 83 | BasicBlock *RetBB = BasicBlock::Create("return", FibF); |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 84 | // Create an exit block. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 85 | BasicBlock* RecurseBB = BasicBlock::Create("recurse", FibF); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 86 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 87 | // Create the "if (arg < 2) goto exitbb" |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 88 | Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond"); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 89 | BranchInst::Create(RetBB, RecurseBB, CondInst, BB); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 90 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 91 | // Create: ret int 1 |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 92 | ReturnInst::Create(One, RetBB); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 93 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 94 | // create fib(x-1) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 95 | Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 96 | Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 97 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 98 | // create fib(x-2) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 99 | Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 100 | Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 101 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 102 | // fib(x-1)+fib(x-2) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 103 | Value *Sum = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 104 | BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 105 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 106 | // Create the return instruction and add it to the basic block |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 107 | ReturnInst::Create(Sum, RecurseBB); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 108 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 109 | return FibF; |
| 110 | } |
| 111 | |
| 112 | struct threadParams { |
| 113 | ExecutionEngine* EE; |
| 114 | Function* F; |
| 115 | int value; |
| 116 | }; |
| 117 | |
| 118 | // We block the subthreads just before they begin to execute: |
| 119 | // we want all of them to call into the JIT at the same time, |
| 120 | // to verify that the locking is working correctly. |
| 121 | class WaitForThreads |
| 122 | { |
| 123 | public: |
| 124 | WaitForThreads() |
| 125 | { |
| 126 | n = 0; |
| 127 | waitFor = 0; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 128 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 129 | int result = pthread_cond_init( &condition, NULL ); |
| 130 | assert( result == 0 ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 131 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 132 | result = pthread_mutex_init( &mutex, NULL ); |
| 133 | assert( result == 0 ); |
| 134 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 135 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 136 | ~WaitForThreads() |
| 137 | { |
| 138 | int result = pthread_cond_destroy( &condition ); |
| 139 | assert( result == 0 ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 140 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 141 | result = pthread_mutex_destroy( &mutex ); |
| 142 | assert( result == 0 ); |
| 143 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 144 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 145 | // All threads will stop here until another thread calls releaseThreads |
| 146 | void block() |
| 147 | { |
| 148 | int result = pthread_mutex_lock( &mutex ); |
| 149 | assert( result == 0 ); |
| 150 | n ++; |
| 151 | //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 152 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 153 | assert( waitFor == 0 || n <= waitFor ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 154 | if ( waitFor > 0 && n == waitFor ) |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 155 | { |
| 156 | // There are enough threads blocked that we can release all of them |
| 157 | std::cout << "Unblocking threads from block()" << std::endl; |
| 158 | unblockThreads(); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 159 | } |
| 160 | else |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 161 | { |
| 162 | // We just need to wait until someone unblocks us |
| 163 | result = pthread_cond_wait( &condition, &mutex ); |
| 164 | assert( result == 0 ); |
| 165 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 166 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 167 | // unlock the mutex before returning |
| 168 | result = pthread_mutex_unlock( &mutex ); |
| 169 | assert( result == 0 ); |
| 170 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 171 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 172 | // If there are num or more threads blocked, it will signal them all |
| 173 | // Otherwise, this thread blocks until there are enough OTHER threads |
| 174 | // blocked |
| 175 | void releaseThreads( size_t num ) |
| 176 | { |
| 177 | int result = pthread_mutex_lock( &mutex ); |
| 178 | assert( result == 0 ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 179 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 180 | if ( n >= num ) { |
| 181 | std::cout << "Unblocking threads from releaseThreads()" << std::endl; |
| 182 | unblockThreads(); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 183 | } |
| 184 | else |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 185 | { |
| 186 | waitFor = num; |
| 187 | pthread_cond_wait( &condition, &mutex ); |
| 188 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 189 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 190 | // unlock the mutex before returning |
| 191 | result = pthread_mutex_unlock( &mutex ); |
| 192 | assert( result == 0 ); |
| 193 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 194 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 195 | private: |
| 196 | void unblockThreads() |
| 197 | { |
| 198 | // Reset the counters to zero: this way, if any new threads |
| 199 | // enter while threads are exiting, they will block instead |
| 200 | // of triggering a new release of threads |
| 201 | n = 0; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 202 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 203 | // Reset waitFor to zero: this way, if waitFor threads enter |
| 204 | // while threads are exiting, they will block instead of |
| 205 | // triggering a new release of threads |
| 206 | waitFor = 0; |
| 207 | |
| 208 | int result = pthread_cond_broadcast( &condition ); |
Chris Lattner | 7098772 | 2008-04-08 05:49:09 +0000 | [diff] [blame] | 209 | assert(result == 0); result=result; |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 210 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 211 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 212 | size_t n; |
| 213 | size_t waitFor; |
| 214 | pthread_cond_t condition; |
| 215 | pthread_mutex_t mutex; |
| 216 | }; |
| 217 | |
| 218 | static WaitForThreads synchronize; |
| 219 | |
| 220 | void* callFunc( void* param ) |
| 221 | { |
| 222 | struct threadParams* p = (struct threadParams*) param; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 223 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 224 | // Call the `foo' function with no arguments: |
| 225 | std::vector<GenericValue> Args(1); |
Reid Spencer | 34bd70d | 2007-03-06 17:24:31 +0000 | [diff] [blame] | 226 | Args[0].IntVal = APInt(32, p->value); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 227 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 228 | synchronize.block(); // wait until other threads are at this point |
| 229 | GenericValue gv = p->EE->runFunction(p->F, Args); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 230 | |
Reid Spencer | 34bd70d | 2007-03-06 17:24:31 +0000 | [diff] [blame] | 231 | return (void*)(intptr_t)gv.IntVal.getZExtValue(); |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Chris Lattner | da06288 | 2009-06-17 16:48:44 +0000 | [diff] [blame] | 234 | int main() { |
| 235 | InitializeNativeTarget(); |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 236 | LLVMContext Context; |
Chris Lattner | da06288 | 2009-06-17 16:48:44 +0000 | [diff] [blame] | 237 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 238 | // Create some module to put our function into it. |
Owen Anderson | 31895e7 | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 239 | Module *M = new Module("test", Context); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 240 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 241 | Function* add1F = createAdd1( M ); |
| 242 | Function* fibF = CreateFibFunction( M ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 243 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 244 | // Now we create the JIT. |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 245 | ExecutionEngine* EE = EngineBuilder(M).create(); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 246 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 247 | //~ std::cout << "We just constructed this LLVM module:\n\n" << *M; |
| 248 | //~ std::cout << "\n\nRunning foo: " << std::flush; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 249 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 250 | // Create one thread for add1 and two threads for fib |
| 251 | struct threadParams add1 = { EE, add1F, 1000 }; |
| 252 | struct threadParams fib1 = { EE, fibF, 39 }; |
| 253 | struct threadParams fib2 = { EE, fibF, 42 }; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 254 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 255 | pthread_t add1Thread; |
| 256 | int result = pthread_create( &add1Thread, NULL, callFunc, &add1 ); |
| 257 | if ( result != 0 ) { |
| 258 | std::cerr << "Could not create thread" << std::endl; |
| 259 | return 1; |
| 260 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 261 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 262 | pthread_t fibThread1; |
| 263 | result = pthread_create( &fibThread1, NULL, callFunc, &fib1 ); |
| 264 | if ( result != 0 ) { |
| 265 | std::cerr << "Could not create thread" << std::endl; |
| 266 | return 1; |
| 267 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 268 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 269 | pthread_t fibThread2; |
| 270 | result = pthread_create( &fibThread2, NULL, callFunc, &fib2 ); |
| 271 | if ( result != 0 ) { |
| 272 | std::cerr << "Could not create thread" << std::endl; |
| 273 | return 1; |
| 274 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 275 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 276 | synchronize.releaseThreads(3); // wait until other threads are at this point |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 277 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 278 | void* returnValue; |
| 279 | result = pthread_join( add1Thread, &returnValue ); |
| 280 | if ( result != 0 ) { |
| 281 | std::cerr << "Could not join thread" << std::endl; |
| 282 | return 1; |
| 283 | } |
Reid Spencer | 6fb0d73 | 2005-07-13 23:20:24 +0000 | [diff] [blame] | 284 | std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 285 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 286 | result = pthread_join( fibThread1, &returnValue ); |
| 287 | if ( result != 0 ) { |
| 288 | std::cerr << "Could not join thread" << std::endl; |
| 289 | return 1; |
| 290 | } |
Reid Spencer | 6fb0d73 | 2005-07-13 23:20:24 +0000 | [diff] [blame] | 291 | std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 292 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 293 | result = pthread_join( fibThread2, &returnValue ); |
| 294 | if ( result != 0 ) { |
| 295 | std::cerr << "Could not join thread" << std::endl; |
| 296 | return 1; |
| 297 | } |
Reid Spencer | 6fb0d73 | 2005-07-13 23:20:24 +0000 | [diff] [blame] | 298 | std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 299 | |
Reid Spencer | e8cdc8b | 2005-07-12 21:51:33 +0000 | [diff] [blame] | 300 | return 0; |
| 301 | } |