Keyboard Shortcuts

File
u :up to issue
m :publish + mail comments
M :edit review message
j / k :jump to file after / before current file
J / K :jump to next file with a comment after / before current file
Side-by-side diff
i :toggle intra-line diffs
e :expand all comments
c :collapse all comments
s :toggle showing all comments
n / p :next / previous diff chunk or comment
N / P :next / previous comment
<Up> / <Down> :next / previous line
<Enter> :respond to / edit current comment
d :mark current comment as done
Issue
u :up to list of issues
m :publish + mail comments
j / k :jump to patch after / before current patch
o / <Enter> :open current patch in side-by-side view
i :open current patch in unified diff view
Issue List
j / k :jump to issue after / before current issue
o / <Enter> :open current issue
# : close issue
Comment/message editing
<Ctrl> + s or <Ctrl> + Enter :save comment
<Esc> :cancel edit
Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(98)
Issues Repositories Search
Open Issues | Closed Issues | All Issues | Sign in with your Google Account to create issues and add comments

Delta Between Two Patch Sets: src/pkg/runtime/plan9/thread.c

Issue 2273041: code review 2273041: Initial Plan9 runtime support for 386. (Closed)
Left Patch Set: code review 2273041: Initial Plan9 runtime support for 386. Created 15 years, 1 month ago
Right Patch Set: code review 2273041: Initial Plan9 runtime support for 386. Created 15 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/runtime/plan9/signals.h ('k') | src/pkg/runtime/runtime.h » ('j') | no next file with change/comment »
('i') | ('e') | ('c') | ('s')
LEFTRIGHT
1 // Copyright 2010 The Go Authors. All rights reserved. 1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 #include "runtime.h" 5 #include "runtime.h"
6 #include "os.h" 6 #include "os.h"
7 7
8 int8 *goos = "plan9"; 8 int8 *goos = "plan9";
9 9
10 void 10 void
11 minit(void) 11 minit(void)
12 { 12 {
13 } 13 }
14 14
15 void 15 void
16 osinit(void) 16 osinit(void)
17 { 17 {
18 } 18 }
19 19
20 void 20 void
21 initsig(int32 queue) 21 initsig(int32 queue)
22 { 22 {
23 } 23 }
24 24
25 void 25 void
26 exit(int32) 26 exit(int32)
27 { 27 {
28 exits(nil); 28 » exits(nil);
29 } 29 }
30 30
31 void 31 void
32 newosproc(M *m, G *g, void *stk, void (*fn)(void)) 32 newosproc(M *m, G *g, void *stk, void (*fn)(void))
33 { 33 {
34 USED(m, g, stk, fn); 34 USED(m, g, stk, fn);
35 » throw("newosproc"); 35 »·······
36 » m->tls[0] = m->id;» // so 386 asm can find it
37 » if(0){
38 » » printf("newosproc stk=%p m=%p g=%p fn=%p rfork=%p id=%d/%d ostk= %p\n",
39 » » » stk, m, g, fn, rfork, m->id, m->tls[0], &m);
40 » }········
41 »·······
42 » if (rfork(RFPROC | RFMEM, stk, m, g, fn) < 0 )
43 » » throw("newosproc: rfork failed");
36 } 44 }
45
46 // Blocking locks.
47
48 // Implement Locks, using semaphores.
49 // l->key is the number of threads who want the lock.
50 // In a race, one thread increments l->key from 0 to 1
51 // and the others increment it from >0 to >1. The thread
52 // who does the 0->1 increment gets the lock, and the
53 // others wait on the semaphore. When the 0->1 thread
54 // releases the lock by decrementing l->key, l->key will
55 // be >0, so it will increment the semaphore to wake up
56 // one of the others. This is the same algorithm used
57 // in Plan 9's user-level locks.
37 58
38 void 59 void
39 lock(Lock *l) 60 lock(Lock *l)
40 { 61 {
41 if(m->locks < 0) 62 if(m->locks < 0)
42 throw("lock count"); 63 throw("lock count");
43 m->locks++; 64 m->locks++;
44 » if(l->key != 0) 65 »·······
45 » » throw("deadlock"); 66 » if(xadd(&l->key, 1) == 1)
46 » l->key = 1; 67 » » return; // changed from 0 -> 1; we hold lock
68 » // otherwise wait in kernel
69 » while(plan9_semacquire(&l->sema, 1) < 0) {
70 » » /* interrupted; try again */
71 » }
47 } 72 }
48 73
49 void 74 void
50 unlock(Lock *l) 75 unlock(Lock *l)
51 { 76 {
52 m->locks--; 77 m->locks--;
53 if(m->locks < 0) 78 if(m->locks < 0)
54 throw("lock count"); 79 throw("lock count");
55 » if(l->key != 1) 80
56 » » throw("unlock of unlocked lock"); 81 » if(xadd(&l->key, -1) == 0)
57 » l->key = 0; 82 » » return; // changed from 1 -> 0: no contention
83 »·······
84 » plan9_semrelease(&l->sema, 1);
58 } 85 }
86
59 87
60 void· 88 void·
61 destroylock(Lock *l) 89 destroylock(Lock *l)
62 { 90 {
63 // nothing 91 » // nothing
92 }
93
94 // User-level semaphore implementation:
95 // try to do the operations in user space on u,
96 // but when it's time to block, fall back on the kernel semaphore k.
97 // This is the same algorithm used in Plan 9.
98 void
99 usemacquire(Usema *s)
100 {
101 » if((int32)xadd(&s->u, -1) < 0)
102 » » while(plan9_semacquire(&s->k, 1) < 0) {
103 » » » /* interrupted; try again */
104 » » }
64 } 105 }
65 106
66 void 107 void
108 usemrelease(Usema *s)
109 {
110 if((int32)xadd(&s->u, 1) <= 0)
111 plan9_semrelease(&s->k, 1);
112 }
113
114
115 // Event notifications.
116 void
67 noteclear(Note *n) 117 noteclear(Note *n)
68 { 118 {
69 » n->lock.key = 0; 119 » n->wakeup = 0;
120 }
121
122 void
123 notesleep(Note *n)
124 {
125 » while(!n->wakeup)
126 » » usemacquire(&n->sema);
70 } 127 }
71 128
72 void 129 void
73 notewakeup(Note *n) 130 notewakeup(Note *n)
74 { 131 {
75 » n->lock.key = 1; 132 » n->wakeup = 1;
133 » usemrelease(&n->sema);
76 } 134 }
77 135
78 void
79 notesleep(Note *n)
80 {
81 if(n->lock.key != 1)
82 throw("notesleep");
83 }
84
LEFTRIGHT
« src/pkg/runtime/plan9/signals.h ('k') | src/pkg/runtime/runtime.h » ('j') | ('i') | ('e') | ('c') | ('s')
Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b

AltStyle によって変換されたページ (->オリジナル) /