Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
cac904b6
Commit
cac904b6
authored
Oct 17, 2008
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add simple synchronization mechanism.
R=rsc DELTA=25 (19 added, 1 deleted, 5 changed) OCL=17346 CL=17346
parent
925454e9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
6 deletions
+24
-6
main.go
usr/r/reflect/main.go
+1
-1
type.go
usr/r/reflect/type.go
+23
-5
No files found.
usr/r/reflect/main.go
View file @
cac904b6
...
...
@@ -39,6 +39,6 @@ func main() {
t
=
reflect
.
ParseTypeString
(
"*(a int8, b int32)"
);
s
=
reflect
.
ToString
(
t
);
print
(
s
,
"
\n
"
);
t
=
reflect
.
ParseTypeString
(
"struct {c *(? *chan *
int32
, ? *int8)}"
);
t
=
reflect
.
ParseTypeString
(
"struct {c *(? *chan *
P.integer
, ? *int8)}"
);
s
=
reflect
.
ToString
(
t
);
print
(
s
,
"
\n
"
);
}
usr/r/reflect/type.go
View file @
cac904b6
...
...
@@ -309,14 +309,30 @@ func NewFuncTypeStruct(in, out *StructTypeStruct) *FuncTypeStruct {
// Cache of expanded types keyed by type name.
var
types
*
map
[
string
]
*
Type
// BUG TODO: should be Type not *Type
// List of typename, typestring pairs
var
typestrings
*
map
[
string
]
string
// Map of basic types to prebuilt StubTypes
var
basicstubs
*
map
[
string
]
*
StubType
var
MissingStub
*
StubType
;
// The database stored in the maps is global; use locking to guarantee safety.
var
lockchan
*
chan
bool
// Channel with buffer of 1, used as a mutex
func
Lock
()
{
lockchan
<-
true
// block if buffer is full
}
func
Unlock
()
{
<-
lockchan
// release waiters
}
func
init
()
{
lockchan
=
new
(
chan
bool
,
1
);
// unlocked at creation - buffer is empty
Lock
();
// not necessary because of init ordering but be safe.
types
=
new
(
map
[
string
]
*
Type
);
typestrings
=
new
(
map
[
string
]
string
);
basicstubs
=
new
(
map
[
string
]
*
StubType
);
...
...
@@ -352,10 +368,9 @@ func init() {
basicstubs
[
"float80"
]
=
NewStubType
(
Float80
);
basicstubs
[
"string"
]
=
NewStubType
(
String
);
typestrings
[
"P.integer"
]
=
"int32"
;
return
;
typestrings
[
"P.S"
]
=
"struct {t *P.T}"
;
typestrings
[
"P.T"
]
=
"struct {c *(? *chan P.S, *int)}"
;
typestrings
[
"P.integer"
]
=
"int32"
;
// TODO: for testing; remove
Unlock
();
}
/*
...
...
@@ -648,7 +663,7 @@ export func ParseTypeString(str string) Type {
return
p
.
Type
()
.
Get
();
}
// Look up type string associated with name.
// Look up type string associated with name.
Lock is held.
func
TypeNameToTypeString
(
name
string
)
string
{
s
,
ok
:=
typestrings
[
name
];
if
!
ok
{
...
...
@@ -660,8 +675,10 @@ func TypeNameToTypeString(name string) string {
// Type is known by name. Find (and create if necessary) its real type.
func
ExpandType
(
name
string
)
Type
{
Lock
();
t
,
ok
:=
types
[
name
];
if
ok
{
Unlock
();
return
*
t
}
types
[
name
]
=
&
Missing
;
// prevent recursion; will overwrite
...
...
@@ -669,5 +686,6 @@ func ExpandType(name string) Type {
p
:=
new
(
Type
);
*
p
=
t1
;
types
[
name
]
=
p
;
Unlock
();
return
t1
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment