 public typealias DispatchClosure = (Void)->(Void)
 public typealias DispatchApplyClosure = (Int)->(Void)
 
-internal var dispatch_time_calc : (NSTimeInterval)->(dispatch_time_t) = { dispatch_time(DISPATCH_TIME_NOW, Int64($0 * NSTimeInterval(NSEC_PER_SEC))) }
+internal func getTimeout(time: NSTimeInterval) -> Int64 { return Int64(time * Double(NSEC_PER_SEC)) }
+
+internal var dispatch_time_calc : (NSTimeInterval)->(dispatch_time_t) = { dispatch_time(DISPATCH_TIME_NOW, getTimeout($0)) }
 
 public enum Queue{
-  
   public enum Atribute {
     static var concurrent : dispatch_queue_attr_t = DISPATCH_QUEUE_CONCURRENT
     static var serial : dispatch_queue_attr_t = DISPATCH_QUEUE_SERIAL

    self.value = value
    semaphore = dispatch_semaphore_create(value)
  }
  public init() {
    self.init(value: 0)
  }
  public func signal() -> Int {
    return dispatch_semaphore_signal(semaphore)
  }
  public func wait() {
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
  }
  public func wait(timeout: NSTimeInterval) -> Int {
    return dispatch_semaphore_wait(semaphore, dispatch_time_calc(timeout))
  }
  
}

public enum Dispatch {
  
  static func async(queue: dispatch_queue_t, closure: DispatchClosure) {
    dispatch_async(queue, closure)
  }
  static func aync(queue: dispatch_queue_t, closure: DispatchClosure) {
    dispatch_sync(queue, closure)
  }
  static func once(inout token: dispatch_once_t, closure: DispatchClosure) {
    dispatch_once(&token, closure)
  }
  static func after(time: NSTimeInterval, queue: dispatch_queue_t, closure: DispatchClosure) {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(time * Double(NSEC_PER_SEC))), queue, closure)
  }
  static func barrierAsync(queue: dispatch_queue_t, closure: DispatchClosure) {
    dispatch_barrier_async(queue, closure)
  }
  static func barrierSync(queue: dispatch_queue_t, closure: DispatchClosure) {
    dispatch_barrier_sync(queue, closure)
  }
  static func apply(iterations: Int, queue: dispatch_queue_t, closure: DispatchApplyClosure) {
    dispatch_apply(iterations, queue, closure)
  }
  static func time(timeout: NSTimeInterval) -> dispatch_time_t {
    return dispatch_time_calc(timeout)
  }
  static var group : Group {
    return Group()
  }
  static func semaphore(value : Int = 0) -> Semaphore {
    return Semaphore(value: value)
  }

}

public struct Group {
  public let group : dispatch_group_t = dispatch_group_create()
  private var onceToken : Int32 = 0
  
  public func enter() {
    dispatch_group_enter(group)
  }
    
  public func leave() {
    dispatch_group_leave(group)
  }
    
  public mutating func enterOnce() {
    enter()
    onceToken = 1
  }
    
  public mutating func leaveOnce() -> Bool {
    guard OSAtomicCompareAndSwapInt(1, 0, &onceToken) else { return false }
    leave()
    return true
  }
    
  public func async(queue: dispatch_queue_t, closure: DispatchClosure) -> Group {
    dispatch_group_async(group, queue) {
      autoreleasepool(closure)
    }
    return self
  }
    
  public func notify(queue: dispatch_queue_t, closure: DispatchClosure) {
    dispatch_group_notify(group, queue) {
      autoreleasepool(closure)
    }
  }
    
  public func wait() {
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER)
  }
    
  public func wait(timeout: NSTimeInterval) -> Int {
    return dispatch_group_wait(group, dispatch_time_calc(timeout))
  }
