本文整理匯總了Golang中sync.NewCond函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewCond函數的具體用法?Golang NewCond怎麽用?Golang NewCond使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewCond函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: newBuffer
func newBuffer(size int64, server *Server) (*buffer, error) {
if size < 0 {
return nil, bufio.ErrNegativeCount
}
if size == 0 {
size = DefaultBufferSize
}
if !powerOfTwo64(size) {
fmt.Printf("Size must be power of two. Try %d.", roundUpPowerOfTwo64(size))
return nil, fmt.Errorf("Size must be power of two. Try %d.", roundUpPowerOfTwo64(size))
}
return &buffer{
id: atomic.AddInt64(&bufcnt, 1),
readIndex: int64(0),
writeIndex: int64(0),
buf: make([]*[]byte, size),
size: size,
mask: size - 1,
pcond: sync.NewCond(new(sync.Mutex)),
ccond: sync.NewCond(new(sync.Mutex)),
_server: server,
b: make([]byte, 5),
}, nil
}
示例2: newFD
func newFD(sysfd int) (*FD, error) {
// Set sysfd to non-blocking mode
err := syscall.SetNonblock(sysfd, true)
if err != nil {
debugf("FD %03d: NF: SetNonBlock: %s", sysfd, err.Error())
return nil, err
}
// Check if sysfd is select-able
// NOTE(npat): Is this useful? Can it ever fail?
var rs fdSet
var tv syscall.Timeval
rs.Zero()
rs.Set(sysfd)
_, err = uxSelect(sysfd+1, &rs, nil, nil, &tv)
if err != nil {
debugf("FD %03d: NF: select(2): %s", sysfd, err.Error())
return nil, err
}
// Initialize FD. We don't generate arbitrary ids, instead we
// use sysfd as the fdMap id. In effect, fd.id is alays ==
// fd.sysfd. Remember that sysfd's can be reused; we must be
// carefull not to allow this to mess things up.
fd := &FD{id: sysfd, sysfd: sysfd}
fd.r.cond = sync.NewCond(&fd.r.mu)
fd.w.cond = sync.NewCond(&fd.w.mu)
// Add to fdMap
fdM.AddFD(fd)
debugf("FD %03d: NF: Ok", fd.id)
return fd, nil
}
示例3: NewConnection
// NewConnection create a new KCP connection between local and remote.
func NewConnection(conv uint16, writerCloser io.WriteCloser, local *net.UDPAddr, remote *net.UDPAddr, block internet.Authenticator) *Connection {
log.Info("KCP|Connection: creating connection ", conv)
conn := new(Connection)
conn.local = local
conn.remote = remote
conn.block = block
conn.writer = writerCloser
conn.since = nowMillisec()
conn.dataInputCond = sync.NewCond(new(sync.Mutex))
conn.dataOutputCond = sync.NewCond(new(sync.Mutex))
authWriter := &AuthenticationWriter{
Authenticator: block,
Writer: writerCloser,
}
conn.conv = conv
conn.output = NewSegmentWriter(authWriter)
conn.mss = authWriter.Mtu() - DataSegmentOverhead
conn.roundTrip = &RoundTripInfo{
rto: 100,
minRtt: effectiveConfig.Tti,
}
conn.interval = effectiveConfig.Tti
conn.receivingWorker = NewReceivingWorker(conn)
conn.fastresend = 2
conn.congestionControl = effectiveConfig.Congestion
conn.sendingWorker = NewSendingWorker(conn)
go conn.updateTask()
return conn
}
示例4: newFD
func newFD(sysfd int) (*FD, error) {
// Set sysfd to non-blocking mode
err := syscall.SetNonblock(sysfd, true)
if err != nil {
debugf("FD xxx: NF: sysfd=%d, err=%v", sysfd, err)
return nil, err
}
// Initialize FD
fd := &FD{sysfd: sysfd}
fd.id = fdM.GetID()
fd.r.cond = sync.NewCond(&fd.r.mu)
fd.w.cond = sync.NewCond(&fd.w.mu)
// Add to Epoll set. We may imediatelly start receiving events
// after this. They will be dropped since the FD is not yet in
// fdMap. It's ok. Nobody is waiting on this FD yet, anyway.
ev := syscall.EpollEvent{
Events: syscall.EPOLLIN |
syscall.EPOLLOUT |
syscall.EPOLLRDHUP |
(syscall.EPOLLET & 0xffffffff),
Fd: int32(fd.id)}
err = syscall.EpollCtl(epfd, syscall.EPOLL_CTL_ADD, fd.sysfd, &ev)
if err != nil {
debugf("FD %03d: NF: sysfd=%d, err=%v", fd.id, fd.sysfd, err)
return nil, err
}
// Add to fdMap
fdM.AddFD(fd)
debugf("FD %03d: NF: sysfd=%d", fd.id, fd.sysfd)
return fd, nil
}
示例5: newBuffer
func newBuffer(size int64) (*buffer, error) {
if size < 0 {
return nil, bufio.ErrNegativeCount
}
if size == 0 {
size = defaultBufferSize
}
if !powerOfTwo64(size) {
return nil, fmt.Errorf("Size must be power of two. Try %d.", roundUpPowerOfTwo64(size))
}
if size < 2*defaultReadBlockSize {
return nil, fmt.Errorf("Size must at least be %d. Try %d.", 2*defaultReadBlockSize, 2*defaultReadBlockSize)
}
return &buffer{
id: atomic.AddInt64(&bufcnt, 1),
buf: make([]byte, size),
size: size,
mask: size - 1,
pseq: newSequence(),
cseq: newSequence(),
pcond: sync.NewCond(new(sync.Mutex)),
ccond: sync.NewCond(new(sync.Mutex)),
cwait: 0,
pwait: 0,
}, nil
}
示例6: New
// New returns new Node instance.
func New(c *Config) (*Node, error) {
if err := os.MkdirAll(c.StateDir, 0700); err != nil {
return nil, err
}
stateFile := filepath.Join(c.StateDir, stateFilename)
dt, err := ioutil.ReadFile(stateFile)
var p []api.Peer
if err != nil && !os.IsNotExist(err) {
return nil, err
}
if err == nil {
if err := json.Unmarshal(dt, &p); err != nil {
return nil, err
}
}
n := &Node{
remotes: newPersistentRemotes(stateFile, p...),
role: ca.WorkerRole,
config: c,
started: make(chan struct{}),
stopped: make(chan struct{}),
closed: make(chan struct{}),
ready: make(chan struct{}),
certificateRequested: make(chan struct{}),
notifyNodeChange: make(chan *api.Node, 1),
}
n.roleCond = sync.NewCond(n.RLocker())
n.connCond = sync.NewCond(n.RLocker())
if err := n.loadCertificates(); err != nil {
return nil, err
}
return n, nil
}
示例7: main
func main() {
var mtx sync.Mutex
var cnd *sync.Cond
var cnds [N]*sync.Cond
var mtxs [N]sync.Mutex
cnd = sync.NewCond(&mtx)
for i := 0; i < N; i++ {
cnds[i] = sync.NewCond(&mtxs[i])
}
for i := 0; i < N; i++ {
go func(me int, m *sync.Mutex, c1 *sync.Cond, c2 *sync.Cond) {
fmt.Printf("Hello, world. %d\n", me)
if me == 0 {
cnd.Signal()
}
for j := 0; j < 10000000; j++ {
m.Lock()
c1.Wait()
m.Unlock()
c2.Signal()
}
if me == N-1 {
cnd.Signal()
}
}(i, &mtxs[i], cnds[i], cnds[(i+1)%N])
}
mtx.Lock()
cnd.Wait()
mtx.Unlock()
cnds[0].Signal()
mtx.Lock()
cnd.Wait()
mtx.Unlock()
}
示例8: iterator
// iterator creates a new iterator for a named auxilary field.
func (a auxIteratorFields) iterator(name string, typ DataType) Iterator {
for _, f := range a {
// Skip field if it's name doesn't match.
// Exit if no points were received by the iterator.
if f.name != name || (typ != Unknown && f.typ != typ) {
continue
}
// Create channel iterator by data type.
switch f.typ {
case Float:
itr := &floatChanIterator{cond: sync.NewCond(&sync.Mutex{})}
f.append(itr)
return itr
case Integer:
itr := &integerChanIterator{cond: sync.NewCond(&sync.Mutex{})}
f.append(itr)
return itr
case String, Tag:
itr := &stringChanIterator{cond: sync.NewCond(&sync.Mutex{})}
f.append(itr)
return itr
case Boolean:
itr := &booleanChanIterator{cond: sync.NewCond(&sync.Mutex{})}
f.append(itr)
return itr
default:
break
}
}
return &nilFloatIterator{}
}
示例9: newSession
func newSession(conn net.Conn, server bool) (*Transport, error) {
var referenceCounter uint64
if server {
referenceCounter = 2
} else {
referenceCounter = 1
}
session := &Transport{
receiverChan: make(chan *channel),
channelC: sync.NewCond(new(sync.Mutex)),
channels: make(map[uint64]*channel),
referenceCounter: referenceCounter,
byteStreamC: sync.NewCond(new(sync.Mutex)),
byteStreams: make(map[uint64]*byteStream),
netConnC: sync.NewCond(new(sync.Mutex)),
netConns: make(map[byte]map[string]net.Conn),
networks: make(map[string]byte),
}
spdyConn, spdyErr := spdystream.NewConnection(conn, server)
if spdyErr != nil {
return nil, spdyErr
}
go spdyConn.Serve(session.newStreamHandler)
session.conn = spdyConn
session.handler = session.initializeHandler()
return session, nil
}
示例10: NewMemQueue
// NewMemQueue returns a new wait queue that holds at most max items.
func NewMemQueue(max int) queue.WaitQueue {
q := &MemQueue{
max: max,
}
q.popCond = sync.NewCond(&q.mu)
q.pushCond = sync.NewCond(&q.mu)
return queue.WithChannel(q)
}
示例11: NewBuffer
func NewBuffer(n int) *Buffer {
b := &Buffer{}
b.capacity = n
b.mu = sync.Mutex{}
b.emptyCond = sync.NewCond(&b.mu)
b.fullCond = sync.NewCond(&b.mu)
return b
}
示例12: newInstance
func newInstance(v *viper.Viper) *instance {
var i = instance{
viper: v,
slots: v.GetInt("start.parallel"),
}
i.slotAvailable = sync.NewCond(&i.m)
i.taskFinished = sync.NewCond(&i.m)
return &i
}
示例13: newPipe
func newPipe(store buffer) (Reader, Writer) {
p := &pipe{}
p.rwait = sync.NewCond(&p.mu)
p.wwait = sync.NewCond(&p.mu)
p.store = store
r := &reader{p}
w := &writer{p}
return r, w
}
示例14: memPipe
func memPipe() (a, b packetConn) {
t1 := memTransport{}
t2 := memTransport{}
t1.write = &t2
t2.write = &t1
t1.Cond = sync.NewCond(&t1.Mutex)
t2.Cond = sync.NewCond(&t2.Mutex)
return &t1, &t2
}
示例15: init
//初始化隊列
func (q *SliceQueue) init(writeslicenum int, sliceelemnum int) {
q.sliceelemnum = sliceelemnum
q.writeslicenum = writeslicenum
q.readwait = sync.NewCond(&sync.Mutex{})
q.writewait = sync.NewCond(&sync.Mutex{})
q.readQueue = q.createSlice()
q.writeQueue = make([]PowerLister, 0)
q.writeQueue = append(q.writeQueue, q.createSlice())
q.run = true
}