I’m looking at the sample code provided in the persistency.pl
and do not quite get the logics behind the use of with_mutex
.
- Why is there
with_mutex(user_db, user_role(Name, Role))
incurrent_user_role/2
butuser_role(Name, Role)
inset_user_role/2
? - Why is there no
with_mutex
inassert_user_role
(add_user/2
)?
Related code fragment is attached:
:- module(user_db,
[ attach_user_db/1, % +File
current_user_role/2, % ?User, ?Role
add_user/2, % +User, +Role
set_user_role/2 % +User, +Role
]).
:- use_module(library(persistency)).
:- persistent
user_role(name:atom, role:oneof([user,administrator])).
attach_user_db(File) :-
db_attach(File, []).
%% current_user_role(+Name, -Role) is semidet.
current_user_role(Name, Role) :-
with_mutex(user_db, user_role(Name, Role)).
add_user(Name, Role) :-
assert_user_role(Name, Role).
set_user_role(Name, Role) :-
user_role(Name, Role), !.
set_user_role(Name, Role) :-
with_mutex(user_db,
( retractall_user_role(Name, _),
assert_user_role(Name, Role))).