/**
 * Warranty & Liability
 * To the extent permitted by applicable law and unless explicitly
 * otherwise agreed upon, XLOG Technologies AG makes no warranties
 * regarding the provided information. XLOG Technologies AG assumes
 * no liability that any problems might be solved with the information
 * provided by XLOG Technologies AG.
 *
 * Rights & License
 * All industrial property rights regarding the information - copyright
 * and patent rights in particular - are the sole property of XLOG
 * Technologies AG. If the company was not the originator of some
 * excerpts, XLOG Technologies AG has at least obtained the right to
 * reproduce, change and translate the information.
 *
 * Reproduction is restricted to the whole unaltered document. Reproduction
 * of the information is only allowed for non-commercial uses. Selling,
 * giving away or letting of the execution of the library is prohibited.
 * The library can be distributed as part of your applications and libraries
 * for execution provided this comment remains unchanged.
 *
 * Restrictions
 * Only to be distributed with programs that add significant and primary
 * functionality to the library. Not to be distributed with additional
 * software intended to replace any components of the library.
 *
 * Trademarks
 * Jekejeke is a registered trademark of XLOG Technologies AG.
 */

/*******************************************************************/
/* Channels                                                        */
/*******************************************************************/

/**
 * chan(C):
 * The predicate succeeds in C with a new channel. The
 * channel can be used from within Dogelog tasks.
 */
% chan(-Object)
chan(C) :-
   os_chan_new(C).

/**
 * send(C, T):
 * The predicate succeeds. As a side effect the term T is
 * sent to the channel C. The predicate blocks until the
 * channel accepts the term.
 */
% send(+Object, +List)
send(C, T) :- ir_is_chan(C), !,
   ir_quote_new(T, H),
   os_chan_put(C, H, P),
   '$YIELD'(P).
send(X, L) :-
   cpu_buf_new(L, H),
   cpu_xbar_put(X, H, P),
   '$YIELD'(P).

/**
 * recv(C, T):
 * The predicate succeeds. As a side effect the term T is
 * received from the channel C. The predicate blocks until
 * the channel provides the term T.
 */
% recv(+Object, -List)
recv(C, T) :- ir_is_chan(C), !,
   os_obj_new(M),
   os_chan_take(C, M, P),
   '$YIELD'(P),
   os_obj_current(M, 'data', H),
   ir_quote_current(H, T).
recv(X, L) :-
   length(L, S),
   os_obj_new(M),
   os_obj_set(M, 'len', S),
   cpu_xbar_take(X, M, P),
   '$YIELD'(P),
   os_obj_current(M, 'len', T),
   (T = S ->
      os_obj_current(M, 'data', H),
      cpu_buf_current(H, L);
      fail).

/*******************************************************************/
/* Tasks                                                           */
/*******************************************************************/

/**
 * create_task(G):
 * create_task(G, T):
 * The predicate succeeds. As a side effect it schedules the
 * goal G to be executed immediately. The binary predicate
 * succeeds in T with the new task.
 */
% create_task(+Goal)
create_task(Goal) :-
   sys_frost_horn((:- Goal), 0, Native),
   os_task_create(Native, _).

% create_task(+Goal, -Task)
create_task(Goal, Task) :-
   sys_frost_horn((:- Goal), 0, Native),
   os_task_create(Native, Task).

/**
 * current_task(T):
 * The predicate succeeds in T with the alive tasks.
 */
% current_task(-Object)
current_task(T) :- var(T), !,
   os_task_list(L),
   member(T, L).
current_task(T) :-
   os_task_alive(T).

/**
 * task_join(T):
 * The predicate succeeds after the task T dies.
 */
% task_join(+Object)
task_join(T) :-
   os_task_join(T, P),
   '$YIELD'(P).

/*******************************************************************/
/* Foreign Predicates                                              */
/*******************************************************************/

% ir_quote_new(T, Q)
% defined in foreign(util/evntlib)

% ir_quote_current(Q, T)
% defined in foreign(util/evntlib)

% os_chan_new(C)
% defined in foreign(util/evntlib)

% ir_is_chan(T)
% defined in foreign(util/evntlib)

% os_chan_put(C, Q, P)
% defined in foreign(util/evntlib)

% os_chan_take(C, M, P)
% defined in foreign(util/evntlib)

% os_task_create(T)
% defined in foreign(util/evntlib)

% os_task_list(T)
% defined in foreign(util/evntlib)

% os_task_alive(T)
% defined in foreign(util/evntlib)

% os_task_join(T, P)
% defined in foreign(util/evntlib)

:- ensure_loaded(foreign(util/evntlib)).