package smb2

import (
	
	
)

type account struct {
	m        sync.Mutex
	balance  chan struct{}
	_opening uint16
}

func openAccount( uint16) *account {
	 := make(chan struct{}, )

	 <- struct{}{} // initial balance

	return &account{
		balance: ,
	}
}

func ( *account) () uint16 {
	return uint16(cap(.balance) - len(.balance))
}

func ( *account) ( uint16,  context.Context) (uint16, bool, error) {
	select {
	case <-.balance:
	case <-.Done():
		return 0, false, &ContextError{Err: .Err()}
	}

	for  := uint16(1);  < ; ++ {
		select {
		case <-.balance:
		default:
			return , false, nil
		}
	}

	return , true, nil
}

func ( *account) () uint16 {
	.m.Lock()

	 := ._opening
	._opening = 0

	.m.Unlock()

	return 
}

func ( *account) (,  uint16) {
	if  == 0 &&  == 0 {
		return
	}

	.m.Lock()

	if  <  {
		._opening +=  - 
	}

	.m.Unlock()

	for  := uint16(0);  < ; ++ {
		select {
		case .balance <- struct{}{}:
		default:
			return
		}
	}
}