-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibc.go
154 lines (134 loc) · 3.15 KB
/
libc.go
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
// message queue
package main
//数据安全不关我的事.jpg
//说起来我为啥要整这个,直接用channel不好么emmmmm
import (
"github.com/kataras/iris/core/errors"
"time"
)
type Node struct{
nextp *Node
value interface{}
lastp *Node
}
type MessageQueue struct{
init bool
length int
now *Node//当前指针
head *Node
in chan interface{}// 传入数据通道
out chan interface{} // 传出数据通道
flag bool//这个我忘了是干啥用的了
flag_int bool//中断标识
}
func (this *MessageQueue)Init()error{
this.in = make(chan interface{})
this.out = make(chan interface{})
this.now = nil
this.flag = false
this.length = 0
this.flag_int = false
go func(){
for{
select {
case value:=<- this.in:
node := &Node{value:value,nextp:this.now,lastp:nil}
if this.flag == true{
this.out<-value
}
if this.length == 0{
this.head = node
this.now = node
}else{
this.now = node
this.now.nextp.lastp = this.now
}
this.length++
}
}
}()
this.init = true
return nil
}
//查询中断状态
func (this *MessageQueue)Status()bool{
return this.flag_int
}
//查询队列内容
func (this *MessageQueue)Length()int{
return this.length
}
// 非堵塞,不管是否成功
func (this *MessageQueue)Enqueue(data interface{}){
//var ok chan bool
go func() {
select{
case this.in<-data:
}
}()
}
//堵塞,等待结果,这个东西似乎没啥用.jpg
func (this *MessageQueue)Enqueue2(data interface{},timeout int)error{
var errorz error
ok:=make(chan bool)
go func() {
select{
case this.in<-data:
errorz = nil
ok<-true
}
}()
select{
case flag:=<-ok:
if flag==true{
return nil
}else{
errorz = errors.New("Have a error")
return errorz
}
}
}
func (this *MessageQueue)Dequeue(timeoutSecs int)(interface{},error){
var errorz error
if this.flag_int{
errorz = errors.New("INT")
return nil,errorz
}
if this.length == 0 && timeoutSecs!=0{
time.Sleep(time.Duration(timeoutSecs))
if this.length == 0{
errorz = errors.New("queue is null")
return nil,errorz
}
}else if timeoutSecs ==0 {
for{
time.Sleep(200)
if this.length !=0{
break//手动堵塞
}else if this.flag_int{
break
}
}
}
value:= this.head
this.head = this.head.lastp
this.length--
return value.value,nil
}
//强制中断
func (this *MessageQueue)INT(yes_or_no bool){
if yes_or_no{
this.flag_int = true
}else{
this.flag_int = false
}
}
func (this *MessageQueue)Clear(){
this.head = nil
this.now = nil
this.length =0
}
func (this *MessageQueue)Save(){
}
func (this *MessageQueue)Load() {
}