github.com/timandy/routine@v1.1.4-0.20240507073150-e4a3e1fe2ba5/api_routine.go (about)

     1  package routine
     2  
     3  // Runnable provides a function without return values.
     4  type Runnable func()
     5  
     6  // Callable provides a function that returns a value of type TResult.
     7  type Callable[TResult any] func() TResult
     8  
     9  // CancelRunnable provides a cancellable function without return values.
    10  type CancelRunnable func(token CancelToken)
    11  
    12  // CancelCallable provides a cancellable function that returns a value of type TResult.
    13  type CancelCallable[TResult any] func(token CancelToken) TResult
    14  
    15  // WrapTask create a new task and capture the inheritableThreadLocals from the current goroutine.
    16  // This function returns a FutureTask instance, but the return task will not run automatically.
    17  // You can run it in a sub-goroutine or goroutine-pool by FutureTask.Run method, wait by FutureTask.Get or FutureTask.GetWithTimeout method.
    18  // When the returned task run panic will be caught and error stack will be printed, the panic will be trigger again when calling FutureTask.Get or FutureTask.GetWithTimeout method.
    19  func WrapTask(fun Runnable) FutureTask[any] {
    20  	ctx := createInheritedMap()
    21  	callable := inheritedTask{context: ctx, function: fun}.run
    22  	return NewFutureTask[any](callable)
    23  }
    24  
    25  // WrapWaitTask create a new task and capture the inheritableThreadLocals from the current goroutine.
    26  // This function returns a FutureTask instance, but the return task will not run automatically.
    27  // You can run it in a sub-goroutine or goroutine-pool by FutureTask.Run method, wait by FutureTask.Get or FutureTask.GetWithTimeout method.
    28  // When the returned task run panic will be caught, the panic will be trigger again when calling FutureTask.Get or FutureTask.GetWithTimeout method.
    29  func WrapWaitTask(fun CancelRunnable) FutureTask[any] {
    30  	ctx := createInheritedMap()
    31  	callable := inheritedWaitTask{context: ctx, function: fun}.run
    32  	return NewFutureTask[any](callable)
    33  }
    34  
    35  // WrapWaitResultTask create a new task and capture the inheritableThreadLocals from the current goroutine.
    36  // This function returns a FutureTask instance, but the return task will not run automatically.
    37  // You can run it in a sub-goroutine or goroutine-pool by FutureTask.Run method, wait and get result by FutureTask.Get or FutureTask.GetWithTimeout method.
    38  // When the returned task run panic will be caught, the panic will be trigger again when calling FutureTask.Get or FutureTask.GetWithTimeout method.
    39  func WrapWaitResultTask[TResult any](fun CancelCallable[TResult]) FutureTask[TResult] {
    40  	ctx := createInheritedMap()
    41  	callable := inheritedWaitResultTask[TResult]{context: ctx, function: fun}.run
    42  	return NewFutureTask[TResult](callable)
    43  }
    44  
    45  // Go starts a new goroutine, and copy inheritableThreadLocals from current goroutine.
    46  // This function will auto invoke the func and print error stack when panic occur in goroutine.
    47  func Go(fun Runnable) {
    48  	task := WrapTask(fun)
    49  	go task.Run()
    50  }
    51  
    52  // GoWait starts a new goroutine, and copy inheritableThreadLocals from current goroutine.
    53  // This function will auto invoke the func and return a FutureTask instance, so we can wait by FutureTask.Get or FutureTask.GetWithTimeout method.
    54  // If panic occur in goroutine, The panic will be trigger again when calling FutureTask.Get or FutureTask.GetWithTimeout method.
    55  func GoWait(fun CancelRunnable) FutureTask[any] {
    56  	task := WrapWaitTask(fun)
    57  	go task.Run()
    58  	return task
    59  }
    60  
    61  // GoWaitResult starts a new goroutine, and copy inheritableThreadLocals from current goroutine.
    62  // This function will auto invoke the func and return a FutureTask instance, so we can wait and get result by FutureTask.Get or FutureTask.GetWithTimeout method.
    63  // If panic occur in goroutine, The panic will be trigger again when calling FutureTask.Get or FutureTask.GetWithTimeout method.
    64  func GoWaitResult[TResult any](fun CancelCallable[TResult]) FutureTask[TResult] {
    65  	task := WrapWaitResultTask(fun)
    66  	go task.Run()
    67  	return task
    68  }