forked from karthick18/inception
-
Notifications
You must be signed in to change notification settings - Fork 1
/
inception.c
1789 lines (1689 loc) · 67.4 KB
/
inception.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* INCEPTION: My tribute to Christopher Nolan
; The code below tries to explain and follow the sequence of events that took place in Inception
; as seen from the eyes of the programmer. It performs the inception through a clever trick
; using x86 code morphing technique in inception.h. This is done so as to make it appear that the thought
; about copying the inception string was done by Fischer himself even though he expected to return to
; a different state in real life.
;
; Copyright (C) 2010-2011 A.R.Karthick
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
;
;
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <assert.h>
#include <sched.h>
#include <sys/mman.h>
#include <time.h>
#ifdef __linux__
#include <syscall.h>
#define GET_TID syscall(SYS_gettid)
#else
#define GET_TID ({0;})
#endif
#include "list.h"
#include "inception_arch.h"
#define DREAM_INCEPTION_TARGET 0x1
#define DREAM_INCEPTION_PERFORMER 0x2
#define DREAM_WORLD_ARCHITECT 0x4
#define DREAM_ORGANIZER 0x8
#define DREAM_SHAPES_FAKER 0x10
#define DREAM_SEDATIVE_CREATOR 0x20
#define DREAM_OVERLOOKER 0x40
#define DREAM_ROLE_MASK 0x7f
#define DREAMERS (0x7)
#define output(fmt, arg...) do { fprintf(stdout, fmt, ##arg);} while(0)
#define DREAM_LEVELS (0x3 + 1 ) /* + 1 as an illustrative considering the 4th is really a limbo from 3rd */
struct dreamer_request
{
#define DREAMER_HIJACKED 0x1
#define DREAMER_DEFENSE_PROJECTIONS (0x2)
#define DREAMER_FREE_FALL (0x4)
#define DREAMER_FAKE_SHAPE (0x8)
#define DREAMER_SHOT (0x10)
#define DREAMER_KILLED (0x20)
#define DREAMER_NEXT_LEVEL (0x40)
#define DREAMER_IN_LIMBO (0x80)
#define DREAMER_IN_MY_DREAM (0x100) /*shared dream*/
#define DREAMER_KICK_BACK (0x200)
#define DREAMER_FIGHT (0x400)
#define DREAMER_SELF (0x800)
#define DREAMER_FALL (0x1000)
#define DREAMER_SYNCHRONIZE_KICK (0x2000)
#define DREAMER_RECOVER (0x4000)
struct dreamer_attr *dattr; /*dreamer attribute*/
int cmd; /* request cmd */
void *arg; /* request cmd arg*/
struct list list; /* list head marker*/
};
struct dreamer_attr
{
const char *name;
int role;
int shared_state; /* shared request command state*/
int level; /*dreamer level*/
struct list_head request_queue; /* per dreamer request queue*/
struct list list; /* list head marker*/
pthread_mutex_t mutex;
pthread_cond_t *cond[DREAM_LEVELS]; /* per dreamer wake up levels */
};
static pthread_mutex_t inception_reality_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t inception_reality_wakeup_for_all = PTHREAD_COND_INITIALIZER;
static struct list_head dreamer_queue[DREAM_LEVELS];
static pthread_mutex_t dreamer_mutex[DREAM_LEVELS];
static pthread_mutex_t limbo_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t limbo_cond = PTHREAD_COND_INITIALIZER;
#define _INCEPTION_C_
#include "inception.h"
static char *fischers_mind_state;
static struct dreamer_attr *fischer_level1;
static pid_t fischer_level1_taskid; /*fischers level1 taskid*/
static int dream_delay_map[DREAM_LEVELS] = { 1, 2, 4, 8};
static int dreamers_in_reality;
static void fischer_dream_level1(void) __attribute__((unused));
/*
* Queue the command to the dreamers request queue
*/
static __inline__ void dream_enqueue_cmd(struct dreamer_attr *dattr, int cmd, void *arg, int level)
{
struct dreamer_request *req = calloc(1, sizeof(*req));
assert(req != NULL);
assert(level > 0 && level <= DREAM_LEVELS);
req->dattr = dattr;
req->cmd = cmd;
req->arg = arg;
pthread_mutex_lock(&dattr->mutex);
list_add_tail(&req->list, &dattr->request_queue);
pthread_cond_signal(dattr->cond[level-1]);
pthread_mutex_unlock(&dattr->mutex);
}
/*
* Clone the request command to all the dreamers: Called with lock held.
*/
static void dream_clone_cmd(struct list_head *dreamer_queue, int cmd, void *arg, struct dreamer_attr *dattr, int level)
{
register struct list *iter;
for(iter = dreamer_queue->head; iter; iter = iter->next)
{
struct dreamer_attr *dreamer = LIST_ENTRY(iter, struct dreamer_attr, list);
if(dattr && dreamer == dattr)
continue; /*skip cloning it on this dreamer*/
dream_enqueue_cmd(dreamer, cmd, arg, level);
}
}
static __inline__ struct dreamer_request *dream_dequeue_cmd(struct dreamer_attr *dattr)
{
struct dreamer_request *req = NULL;
struct list *head = NULL;
pthread_mutex_lock(&dattr->mutex);
if(!dattr->request_queue.nodes)
{
pthread_mutex_unlock(&dattr->mutex);
return NULL;
}
head = dattr->request_queue.head;
assert(head != NULL);
req = LIST_ENTRY(head, struct dreamer_request, list);
list_del(head, &dattr->request_queue);
pthread_mutex_unlock(&dattr->mutex);
return req;
}
static struct dreamer_attr *dreamer_find(struct list_head *dreamer_queue, const char *name, int role)
{
register struct list *iter;
for(iter = dreamer_queue->head; iter ; iter = iter->next)
{
struct dreamer_attr *dattr = LIST_ENTRY(iter, struct dreamer_attr, list);
if(role && (dattr->role & role))
return dattr;
if(name &&
!strcasecmp(dattr->name, name))
return dattr;
}
return NULL;
}
static struct dreamer_attr *dreamer_find_sync_locked(struct dreamer_attr *dreamer, int level, const char *name, int role)
{
struct dreamer_attr *dattr = NULL;
if(!level) return NULL;
rescan:
dattr = dreamer_find(&dreamer_queue[level-1], name, role);
if(!dattr)
{
static int c;
pthread_mutex_unlock(&dreamer_mutex[level-1]);
if(++c >= 10)
{
output("[%s] waiting for [%s] to join at level [%d]\n", dreamer->name,
name ?:"Unknown", level);
}
usleep(100000);
pthread_mutex_lock(&dreamer_mutex[level-1]);
goto rescan;
}
return dattr;
}
static struct dreamer_attr *dreamer_find_sync(struct dreamer_attr *dreamer, int level, const char *name, int role)
{
struct dreamer_attr *dattr;
if(!level) return NULL;
pthread_mutex_lock(&dreamer_mutex[level-1]);
dattr = dreamer_find_sync_locked(dreamer, level, name, role);
pthread_mutex_unlock(&dreamer_mutex[level-1]);
return dattr;
}
/*
* In a dream, you run 12 times slower : 5 mins of realtime = 60 mins
* Fake the slowness by reduction in threads priority or the
* scheduling priority of the dreamer
*/
static void set_thread_priority(struct dreamer_attr *dattr, int level)
{
struct sched_param dream_param = {0};
int policy = 0;
assert(pthread_getschedparam(pthread_self(), &policy, &dream_param) == 0);
if(dream_param.sched_priority > 12)
dream_param.sched_priority -= 12;
output("Dreamer [%s], level [%d], priority [%d], policy [%s]\n",
dattr->name, level, dream_param.sched_priority,
policy == SCHED_FIFO ? "FIFO" :
(policy == SCHED_RR ? "RR" : "OTHER"));
assert(pthread_setschedparam(pthread_self(), policy, &dream_param) == 0);
}
/*
* Wake up an individual dreamer in one level and let him propagate the kick down to levels below.
*/
static void wake_up_dreamer(struct dreamer_attr *dattr, int level)
{
register struct list *iter;
if(!level || (dattr->shared_state & DREAMER_IN_LIMBO)) return;
pthread_mutex_lock(&dreamer_mutex[level-1]);
for(iter = dreamer_queue[level-1].head; iter; iter = iter->next)
{
struct dreamer_attr *dreamer = LIST_ENTRY(iter, struct dreamer_attr, list);
if( !(dreamer->role ^ dattr->role) )
{
dream_enqueue_cmd(dreamer, DREAMER_KICK_BACK, NULL, dreamer->level);
break;
}
}
pthread_mutex_unlock(&dreamer_mutex[level-1]);
}
/*
* Level 0 is wake up dreamers on all levels.
* Skip guys in a limbo from that level to all the way down.
*/
static void wake_up_dreamers(int level)
{
int start = DREAM_LEVELS-1,end = 0;
register int i;
if(level > 0)
{
start = level - 1;
end = level - 1;
}
for(i = start; i >= end; --i)
{
struct list *iter;
pthread_mutex_lock(&dreamer_mutex[i]);
for(iter = dreamer_queue[i].head; iter; iter = iter->next)
{
struct dreamer_attr *dattr = LIST_ENTRY(iter, struct dreamer_attr, list);
if( (dattr->shared_state & DREAMER_IN_LIMBO) )
continue;
dream_enqueue_cmd(dattr, DREAMER_KICK_BACK, NULL, dattr->level);
}
pthread_mutex_unlock(&dreamer_mutex[i]);
}
}
/*
* Update states on all the levels and down.
*/
static void set_state(struct dreamer_attr *dattr, int state)
{
register int i;
for(i = DREAM_LEVELS - 1; i >= 0; --i)
{
register struct list *iter;
pthread_mutex_lock(&dreamer_mutex[i]);
for(iter = dreamer_queue[i].head; iter; iter = iter->next)
{
struct dreamer_attr *dreamer = LIST_ENTRY(iter, struct dreamer_attr, list);
if(!(dreamer->role ^ dattr->role))
{
dreamer->shared_state |= state;
break;
}
}
pthread_mutex_unlock(&dreamer_mutex[i]);
}
}
/*
* Set the limbo state on all levels of this dreamer so he cannot get a kick back
* Called with the lock on that level.
*/
static void set_limbo_state(struct dreamer_attr *dattr)
{
set_state(dattr, DREAMER_IN_LIMBO);
}
/*
* Called with the dreamer mutex held for the appropriate level.
*/
static void wait_for_kick(struct dreamer_attr *dattr)
{
struct dreamer_request *req = NULL;
for(;;)
{
struct timespec ts = {0};
while ( (req = dream_dequeue_cmd(dattr)) )
{
if(req->cmd == DREAMER_KICK_BACK)
{
free(req);
if(dattr->level > 1)
{
output("[%s] got Kick at level [%d]. Exiting back to level [%d]\n",
dattr->name, dattr->level, dattr->level - 1);
}
else
{
output("[%s] got Kick at level [%d]. Exiting back to reality\n",
dattr->name, dattr->level);
}
goto out;
}
free(req);
}
arch_gettime(dream_delay_map[dattr->level-1], &ts);
pthread_cond_timedwait(dattr->cond[dattr->level-1], &dreamer_mutex[dattr->level-1], &ts);
}
out:
return;
}
static struct dreamer_attr *dream_attr_clone(int level, struct dreamer_attr *dattr)
{
struct dreamer_attr *dattr_clone = calloc(1, sizeof(*dattr_clone));
assert(dattr_clone != NULL);
memcpy(dattr_clone, dattr, sizeof(*dattr_clone));
dattr_clone->level = level;
memset(&dattr_clone->mutex, 0, sizeof(dattr_clone->mutex));
assert(pthread_mutex_init(&dattr_clone->mutex, NULL) == 0);
list_init(&dattr_clone->request_queue);
return dattr_clone;
}
static __inline__ void dream_level_create(int level, void * (*dream_function) (void *), struct dreamer_attr *dattr)
{
pthread_attr_t attr;
pthread_t dream;
struct dreamer_attr *dattr_clone = dream_attr_clone(level, dattr);
assert(dattr_clone != NULL);
assert(pthread_attr_init(&attr)==0);
assert(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) == 0);
assert(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED)==0);
assert(pthread_create(&dream, &attr, dream_function, dattr_clone) == 0);
}
/*
* Now this is the state where Cobb. meets Saito.
* The beauty of the Films ending is: Did Cobb take a kick back to reality on seeing Saito remind him
* that he has been in a limbo with his wife and has to come back to become Young.
* And does Saito take the kick back when Cobb. pulls the trigger on him implicitly in limbo to give him the kick back.
* Either way based on whether Cobb. got the kick back from limbo or not, the end is a reality or limbo.
* Thats the ingenuity of Inception. So I think its better if we don't mess this up for ourselves and
* just sleep here till infinity!
*/
static void infinite_subconsciousness(struct dreamer_attr *dattr)
{
struct timespec ts = {0};
static int dreamers;
pthread_mutex_lock(&limbo_mutex);
++dreamers;
while(dreamers != 2)
{
arch_gettime(2, &ts);
pthread_cond_timedwait(&limbo_cond, &limbo_mutex, &ts);
}
/*
* Wait for the signal from Fischer
*/
while(!dreamers_in_reality)
{
arch_gettime(1, &ts);
pthread_cond_timedwait(&limbo_cond, &limbo_mutex, &ts);
}
if((dattr->role & DREAM_INCEPTION_PERFORMER))
{
output("\n\nCobbs' search for Saito ends in Limbo. Now either both take the kick back to reality "
"or the ending is still a state of limbo from Cobbs' perspective, which is what Nolan wants us to think\n");
output("This is in spite of witnessing his children turn towards him for the first time which we're never shown in his projections.\n");
output("So, let me end the limbo state abruptly like the Movie with the totem spinning and leave it to the reviewers to decide the infinite sleep:-)\n\n");
}
pthread_cond_signal(&limbo_cond);
pthread_mutex_unlock(&limbo_mutex);
sleep(1<<31);
}
/*
* TODO next: Limbo is a state of infinite subconciousness
*/
static void enter_limbo(struct dreamer_attr *dattr)
{
struct dreamer_attr *clone = NULL;
struct dreamer_request *req = NULL;
dattr->shared_state |= DREAMER_IN_LIMBO;
clone = dream_attr_clone(dattr->level+1, dattr);
assert(clone != NULL);
pthread_mutex_lock(&dreamer_mutex[3]);
list_add_tail(&clone->list, &dreamer_queue[3]);
while( (dreamer_queue[3].nodes + 3) != DREAMERS)
{
pthread_mutex_unlock(&dreamer_mutex[3]);
usleep(100000);
pthread_mutex_lock(&dreamer_mutex[3]);
}
switch( (clone->role & DREAM_ROLE_MASK) )
{
case DREAM_INCEPTION_PERFORMER: /* Cobb */
{
struct timespec ts;
struct dreamer_attr *ariadne = NULL;
int inception_done = 0;
int search_for_saito = 0;
ariadne = dreamer_find(&dreamer_queue[3], "ariadne", DREAM_WORLD_ARCHITECT);
/*
* Self enqueue Mal and her thoughts into the dream
*/
dream_enqueue_cmd(clone, DREAMER_IN_MY_DREAM, (void*)"Cobb takes Elevator to meet Mal", clone->level);
dream_enqueue_cmd(clone, DREAMER_IN_MY_DREAM,
(void*)"[Cobb] tells [Mal] about his inception on her to think that the WORLD is unreal",
clone->level);
dream_enqueue_cmd(clone, DREAMER_IN_MY_DREAM,
(void*)"[Mal] wants [Cobb] to go back with him into the world they built in their dreams",
clone->level);
for(;;)
{
while( (req = dream_dequeue_cmd(clone)) )
{
/*
* Meets Mal
*/
if(req->cmd == DREAMER_IN_MY_DREAM)
{
output("%s in level [%d] while in limbo\n", (const char*)req->arg, clone->level);
}
/*
* Mal killed
*/
else if(req->cmd == DREAMER_KILLED)
{
output("[%s] finds %s in level [%d] while in limbo\n",
clone->name, (const char*)req->arg, clone->level);
}
/*
* Recover to search for Saito. Here mark the inception
*/
else if(req->cmd == DREAMER_RECOVER)
{
struct dreamer_attr *source = (struct dreamer_attr*)req->arg;
/*
* If the recovery trigger is from ariadne
*/
if( (source->role & DREAM_WORLD_ARCHITECT) )
{
if(!inception_done)
{
search_for_saito = 1;
}
else
{
search_saito:
output("[%s] enters limbo to search for Saito in limbo at level [%d]\n",
clone->name, clone->level);
pthread_mutex_unlock(&dreamer_mutex[3]);
set_limbo_state(clone);
usleep(10000);
infinite_subconsciousness(clone);
pthread_mutex_lock(&dreamer_mutex[3]);
output("[%s] returned after searching for Saito in limbo at level [%d]\n",
clone->name, clone->level);
assert(0); /* should not return back here*/
}
}
/*
* Indicator from Fischer for the final shot.
*/
else if( (source->role & DREAM_INCEPTION_TARGET) )
{
inception_done = 1;
memcpy(fischers_mind_state, inception_thoughts, sizeof(inception_thoughts));
/*
* Send recovery indicator to Ariadne
*/
dream_enqueue_cmd(ariadne, DREAMER_RECOVER, clone, ariadne->level);
if(search_for_saito)
{
goto search_saito;
}
}
}
}
arch_gettime(dream_delay_map[clone->level-1], &ts);
pthread_cond_timedwait(clone->cond[3], &dreamer_mutex[3], &ts);
}
}
break;
case DREAM_WORLD_ARCHITECT: /* Ariadne */
{
struct dreamer_attr *cobb = NULL;
struct dreamer_attr *fischer = NULL;
struct timespec ts = {0};
cobb = dreamer_find(&dreamer_queue[3], "cobb", DREAM_INCEPTION_PERFORMER);
fischer = dreamer_find(&dreamer_queue[3], "fischer", DREAM_INCEPTION_TARGET);
/*
* Self enqueue to follow Cobb. in the Elevator to his wife.
*/
dream_enqueue_cmd(clone, DREAMER_IN_MY_DREAM, cobb, clone->level);
for(;;)
{
while ( (req = dream_dequeue_cmd(clone) ) )
{
if(req->cmd == DREAMER_IN_MY_DREAM)
{
output("[%s] follows [%s] in Elevator to level [%d] in Limbo to meet his wife\n",
clone->name, cobb->name, clone->level);
pthread_mutex_unlock(&dreamer_mutex[3]);
/*
* Take a breather while Cobb. interacts with his and tells her about his inception.
*/
sleep(dream_delay_map[clone->level-1]);
pthread_mutex_lock(&dreamer_mutex[3]);
dream_enqueue_cmd(cobb, DREAMER_KILLED, (void*)"[Mal] killed", cobb->level);
pthread_mutex_unlock(&dreamer_mutex[3]);
/*
* Quick breather
*/
usleep(10000);
pthread_mutex_lock(&dreamer_mutex[3]);
/*
* Now send Fischer a kick back from limbo down to reconcile.
*/
dream_enqueue_cmd(fischer, DREAMER_KICK_BACK, clone, fischer->level);
/*
* Now tell Cobb. to recover and go and search Saito as he is the only one who can
* search her in limbo.
*/
output("[%s] tells [%s] to search for Saito in limbo at level [%d]\n",
clone->name, cobb->name, clone->level);
dream_enqueue_cmd(cobb, DREAMER_RECOVER, clone, cobb->level);
}
else if(req->cmd == DREAMER_RECOVER)
{
/*
* Indication for us to take the kick back.
*/
dream_enqueue_cmd(clone, DREAMER_KICK_BACK, clone, clone->level);
}
else if(req->cmd == DREAMER_KICK_BACK)
{
struct dreamer_attr *self = NULL; /*ourself in lower level*/
/*
* Return back
*/
dattr->shared_state &= ~DREAMER_IN_LIMBO;
clone->shared_state &= ~DREAMER_IN_LIMBO;
pthread_mutex_unlock(&dreamer_mutex[3]);
free(req);
usleep(10000);
self = dreamer_find_sync(clone, clone->level-1, "ariadne", DREAM_WORLD_ARCHITECT);
dream_enqueue_cmd(self, DREAMER_KICK_BACK, clone, self->level);
output("[%s] taking the kick back from limbo at level [%d] to level [%d]\n",
clone->name, clone->level, clone->level-1);
goto out;
}
free(req);
}
arch_gettime(dream_delay_map[clone->level-1], &ts);
pthread_cond_timedwait(clone->cond[3], &dreamer_mutex[3], &ts);
}
}
break;
case DREAM_OVERLOOKER: /* Saito */
{
pthread_mutex_unlock(&dreamer_mutex[3]);
set_limbo_state(clone);
usleep(1000);
infinite_subconsciousness(clone);
}
break;
case DREAM_INCEPTION_TARGET: /*Fischer*/
{
struct dreamer_attr *self = NULL;
struct timespec ts = {0};
pthread_mutex_unlock(&dreamer_mutex[3]);
/*
* Find ourselves in the lower level to take the kick back.
*/
self = dreamer_find_sync(clone, clone->level-1, "fischer", DREAM_INCEPTION_TARGET);
pthread_mutex_lock(&dreamer_mutex[3]);
for(;;)
{
while ( (req = dream_dequeue_cmd(clone)) )
{
if(req->cmd == DREAMER_KICK_BACK)
{
dattr->shared_state &= ~DREAMER_IN_LIMBO;
clone->shared_state &= ~DREAMER_IN_LIMBO;
pthread_mutex_unlock(&dreamer_mutex[3]);
free(req);
pthread_mutex_lock(&dreamer_mutex[clone->level-2]);
dream_enqueue_cmd(self, DREAMER_KICK_BACK, clone, self->level);
pthread_mutex_unlock(&dreamer_mutex[clone->level-2]);
output("[%s] kicking off from limbo at [%d] to level [%d]\n",
clone->name, clone->level, clone->level-1);
goto out;
}
free(req);
}
arch_gettime(dream_delay_map[clone->level-1], &ts);
pthread_cond_timedwait(clone->cond[3], &dreamer_mutex[3], &ts);
}
}
break;
}
pthread_mutex_unlock(&dreamer_mutex[3]);
out:
return ;
}
/*
* The last level of the dream beyond which we enter limbo.
*/
static void *dream_level_3(void *arg)
{
struct dreamer_attr *dattr = arg;
struct dreamer_request *req = NULL;
assert(dattr->level == 3);
set_thread_priority(dattr, 3);
pthread_mutex_lock(&dreamer_mutex[2]);
list_add_tail(&dattr->list, &dreamer_queue[2]);
while( (dreamer_queue[2].nodes + 2 != DREAMERS ) )
{
pthread_mutex_unlock(&dreamer_mutex[2]);
usleep(10000);
pthread_mutex_lock(&dreamer_mutex[2]);
}
/*
* All have joined in level 3
*/
switch( (dattr->role & DREAM_ROLE_MASK) )
{
case DREAM_INCEPTION_PERFORMER: /* Cobb */
{
struct dreamer_attr *fischer = NULL;
struct dreamer_attr *ariadne = NULL;
struct dreamer_attr *eames = NULL;
/*
* Self enqueue
*/
fischer = dreamer_find(&dreamer_queue[2], "fischer", DREAM_INCEPTION_TARGET);
ariadne = dreamer_find(&dreamer_queue[2], "ariadne", DREAM_WORLD_ARCHITECT);
eames = dreamer_find(&dreamer_queue[2], "eames", DREAM_SHAPES_FAKER);
dream_enqueue_cmd(dattr, DREAMER_FIGHT, NULL, dattr->level);
dream_enqueue_cmd(dattr, DREAMER_IN_MY_DREAM, (void*)"Mal", dattr->level);
for(;;)
{
while ( (req = dream_dequeue_cmd(dattr)))
{
if(req->cmd == DREAMER_FIGHT)
{
output("[%s] fights Fischers defense projections in level [%d]\n",
dattr->name, dattr->level);
}
else if(req->cmd == DREAMER_IN_MY_DREAM)
{
output("[%s] sees his wife [%s] in his dream. [%s] shoots Fischer\n",
dattr->name, (char*)req->arg, (char*)req->arg);
dream_enqueue_cmd(fischer, DREAMER_SHOT, (void*)"Mal", fischer->level);
/*
* Let Eames know so he could start recovery on Fischer
*/
dream_enqueue_cmd(eames, DREAMER_SHOT, fischer, dattr->level);
/*
* Ask Ariadne to follow me in limbo.
*/
output("[%s] asks [%s] to follow him in limbo with his wife\n",
dattr->name, ariadne->name);
dream_enqueue_cmd(ariadne, DREAMER_IN_MY_DREAM, dattr, dattr->level);
/*
* Give up the lock and take a break before trying to grab the lock again
*/
pthread_mutex_unlock(&dreamer_mutex[2]);
usleep(100000);
pthread_mutex_lock(&dreamer_mutex[2]);
/*
* Self enqueue to get into limbo.
*/
dream_enqueue_cmd(dattr, DREAMER_IN_LIMBO, dattr, dattr->level);
}
else if(req->cmd == DREAMER_IN_LIMBO)
{
output("[%s] enters limbo with his Wifes projections in level [%d]\n",
dattr->name, dattr->level);
pthread_mutex_unlock(&dreamer_mutex[2]);
enter_limbo(dattr);
pthread_mutex_lock(&dreamer_mutex[2]);
/*
* should not be reached
*/
output("[%s] returned from limbo. Exiting out at level [%d]\n", dattr->name,
dattr->level);
exit(0);
}
free(req);
}
pthread_mutex_unlock(&dreamer_mutex[2]);
sleep(2);
pthread_mutex_lock(&dreamer_mutex[2]);
}
}
break;
case DREAM_WORLD_ARCHITECT: /* Ariadne*/
{
/*
* Wait for Cobbs command to enter his dream in limbo with him.
*/
struct timespec ts = {0};
int ret_from_limbo = 0;
for(;;)
{
while ( (req = dream_dequeue_cmd(dattr)) )
{
if(req->cmd == DREAMER_IN_MY_DREAM)
{
output("[%s] enters with Cobb. to meet his wife in Limbo at level [%d]\n",
dattr->name, dattr->level);
pthread_mutex_unlock(&dreamer_mutex[2]);
enter_limbo(dattr);
pthread_mutex_lock(&dreamer_mutex[2]);
}
else if(req->cmd == DREAMER_KICK_BACK)
{
if(!ret_from_limbo)
{
struct dreamer_attr *yusuf = NULL;
ret_from_limbo = 1;
output("[%s] returned from Fischers limbo to level [%d] to take the synchronized kick\n",
dattr->name, dattr->level);
pthread_mutex_unlock(&dreamer_mutex[2]);
pthread_mutex_lock(&dreamer_mutex[0]);
yusuf = dreamer_find_sync_locked(dattr, 1, "yusuf", DREAM_SEDATIVE_CREATOR);
dream_enqueue_cmd(yusuf, DREAMER_SYNCHRONIZE_KICK, dattr, yusuf->level);
pthread_mutex_unlock(&dreamer_mutex[0]);
/*
* Take a breather while Yusuf does his work so we can rescan for a kick back
* Otherwise we miss and get it after our delayed sleep
*/
usleep(10000);
pthread_mutex_lock(&dreamer_mutex[2]);
}
else
{
free(req);
output("[%s] got Kick back from level [%d]. Exiting back to level [%d]\n",
dattr->name, dattr->level, dattr->level-1);
goto out_unlock;
}
}
free(req);
}
arch_gettime(dream_delay_map[dattr->level-1], &ts);
pthread_cond_timedwait(dattr->cond[2], &dreamer_mutex[2], &ts);
}
}
break;
case DREAM_SHAPES_FAKER: /* Eames*/
{
struct timespec ts = {0};
struct dreamer_attr *fischer = NULL;
struct dreamer_attr *saito = NULL;
saito = dreamer_find(&dreamer_queue[2], "saito", DREAM_OVERLOOKER);
/*
* Self enqueue and he is the dreamer at this level
*/
dream_enqueue_cmd(dattr, DREAMER_FIGHT, dattr, dattr->level);
/*
* Ask Saito to fight first before he is killed!
*/
dream_enqueue_cmd(saito, DREAMER_FIGHT, dattr, dattr->level);
dream_enqueue_cmd(saito, DREAMER_KILLED, saito, dattr->level);
for(;;)
{
while ( (req = dream_dequeue_cmd(dattr)))
{
if(req->cmd == DREAMER_FIGHT)
{
output("[%s] fights Fischers defense projections at level [%d]\n",
dattr->name, dattr->level);
}
else if(req->cmd == DREAMER_SHOT)
{
fischer = ( (struct dreamer_attr*)req->arg);
output("[%s] sees [%s] shot in level [%d]. Starts recovery\n",
dattr->name, fischer->name, dattr->level);
dream_enqueue_cmd(dattr, DREAMER_RECOVER, fischer, dattr->level);
}
else if(req->cmd == DREAMER_RECOVER)
{
output("[%s] doing recovery on [%s] who is shot at level [%d]\n",
dattr->name, ( (struct dreamer_attr*)req->arg)->name, dattr->level);
}
else if(req->cmd == DREAMER_KICK_BACK)
{
struct dreamer_attr *src = (struct dreamer_attr*)req->arg;
if(src && (src->role & DREAM_INCEPTION_TARGET))
{
output("[%s] sees [%s] get a recovery kick at level [%d]. "
"Starts faking Fischers Father's projections for the final Inception\n",
dattr->name, src->name, src->level);
dream_enqueue_cmd(src, DREAMER_FAKE_SHAPE, "Maurice Fischer", src->level);
}
else
{
free(req);
output("[%s] got Kick at level [%d]. Exiting back to level [%d]\n",
dattr->name, dattr->level, dattr->level - 1);
goto out_unlock;
}
}
free(req);
}
if(fischer)
{
/*
* Keep recovering fischer if he is shot in this level.
*/
dream_enqueue_cmd(dattr, DREAMER_RECOVER, fischer, dattr->level);
}
arch_gettime(dream_delay_map[dattr->level-1], &ts);
pthread_cond_timedwait(dattr->cond[2], &dreamer_mutex[2], &ts);
}
}
break;
case DREAM_OVERLOOKER: /*Saito*/
{
struct timespec ts = {0};
for(;;)
{
while( (req = dream_dequeue_cmd(dattr)))
{
if(req->cmd == DREAMER_FIGHT)
{
output("[%s] fights Fischers projections in level [%d]\n",
dattr->name, dattr->level);
}
else if(req->cmd == DREAMER_KILLED) /* Killed. Enter limbo */
{
output("[%s] gets killed at level [%d]. Enters limbo\n", dattr->name, dattr->level);
pthread_mutex_unlock(&dreamer_mutex[2]);
/*
* Update killed status on all the levels. just for the sake of being
* consistent
*/
set_state(dattr, DREAMER_KILLED);
enter_limbo(dattr);
pthread_mutex_lock(&dreamer_mutex[2]);
/*
* Unreached.
*/
output("[%s] returned back from Limbo at level [%d]\n", dattr->name, dattr->level);
exit(0);
}
free(req);
}
arch_gettime(dream_delay_map[dattr->level-1], &ts);
pthread_cond_timedwait(dattr->cond[2], &dreamer_mutex[2], &ts);
}
}
break;
case DREAM_INCEPTION_TARGET: /* Fischer */
{
int reconciled = 0;
struct timespec ts = {0};
for(;;)
{
while ( (req = dream_dequeue_cmd(dattr)))
{
if(req->cmd == DREAMER_SHOT)
{
output("[%s] shot by [%s] in level [%d]\n",
dattr->name, (char*)req->arg, dattr->level);
/*
* Freeze for sometime before joining Cobb and Ariadne in limbo.
*/
pthread_mutex_unlock(&dreamer_mutex[2]);
usleep(100000);
enter_limbo(dattr);
pthread_mutex_lock(&dreamer_mutex[2]);
}
else if(req->cmd == DREAMER_KICK_BACK)
{
if(!reconciled)
{
struct dreamer_attr *eames = NULL;
output("[%s] got a kick back from Limbo at level [%d]\n", dattr->name, dattr->level);
eames = dreamer_find(&dreamer_queue[2], "eames", DREAM_SHAPES_FAKER);
dream_enqueue_cmd(eames, DREAMER_KICK_BACK, dattr, eames->level);
}
else
{
free(req);
output("[%s] got a kick at level [%d]. Falling back to level [%d]\n",
dattr->name, dattr->level, dattr->level - 1);
goto out_unlock;
}
}
/*
* Fischer meeting with his dying father (reconciliation phase at the lowest level)
*/
else if(req->cmd == DREAMER_FAKE_SHAPE)
{
struct dreamer_attr *cobb = NULL;
reconciled = 1;
pthread_mutex_unlock(&dreamer_mutex[2]);
usleep(10000); /*take a breather*/
output("[%s] going to meet his dying father [%s] after getting a kick back to level [%d]\n",
dattr->name, (const char*)req->arg, dattr->level);
/*
* Indicator to Cobb. for you know WHAT :-)
*/
pthread_mutex_lock(&dreamer_mutex[dattr->level]);
cobb = dreamer_find_sync_locked(dattr, dattr->level+1, "cobb", DREAM_INCEPTION_PERFORMER);
dream_enqueue_cmd(cobb, DREAMER_RECOVER, dattr, cobb->level);
pthread_mutex_unlock(&dreamer_mutex[dattr->level]);
pthread_mutex_lock(&dreamer_mutex[2]);
}
free(req);
}
arch_gettime(dream_delay_map[dattr->level-1], &ts);
pthread_cond_timedwait(dattr->cond[2], &dreamer_mutex[2], &ts);
}
}
break;
default:
break;
}
out_unlock:
pthread_mutex_unlock(&dreamer_mutex[2]);
wake_up_dreamer(dattr, 2);
return NULL;
}
static void *dream_level_2(void *arg)
{
struct dreamer_attr *dattr = arg;
int dreamers = 0;
assert(dattr->level == 2);
set_thread_priority(dattr, 2);
/*
* take level 2 lock.
*/
pthread_mutex_lock(&dreamer_mutex[1]);
list_add_tail(&dattr->list, &dreamer_queue[1]);
/*
* Wait for the expected members to join at this level.
*/
while( (dreamers = dreamer_queue[1].nodes)+1 != DREAMERS)
{
pthread_mutex_unlock(&dreamer_mutex[1]);