1LIBDLM(3) Library Functions Manual LIBDLM(3)
2
3
4
6 libdlm - dlm_get_fd, dlm_dispatch, dlm_pthread_init,
7 dlm_ls_pthread_init, dlm_cleanup
8
10 #include <libdlm.h>
11 int dlm_pthread_init();
12 int dlm_ls_pthread_init(dlm_lshandle_t lockspace);
13 int dlm_pthread_cleanup();
14 int dlm_get_fd(void);
15 int dlm_dispatch(int fd);
16
17 link with -ldlm
18
20 libdlm provides the programmatic userspace interface to the Distributed
21 Lock manager. It provides all the calls you need to manipulate locks &
22 lockspaces
23 libdlm can be used in pthread or non-pthread applications. For pthread
24 applications simply call the following function before doing any lock
25 operations. If you're using pthreads, remember to define _REENTRANT at
26 the top of the program or using -D_REENTRANT on the compile line.
27 pthreads is the normal way of using the DLM. This way you simply ini‐
28 tialize the DLM's thread and all the AST routines will be delivered in
29 that thread. You just call the dlm_lock() etc routines in the main line
30 of your program.
31 If you don't want to use pthreads or you want to handle the dlm call‐
32 back ASTs yourself then you can get an FD handle to the DLM device and
33 call dlm_dispatch() on it whenever it becomes active. That was ASTs
34 will be delivered in the context of the thread/process that called
35 dlm_dispatch().
36
37
38
39 int dlm_pthread_init()
40 Creates a thread to receive all lock ASTs. The AST callback function
41 for lock operations will be called in the context of this thread. If
42 there is a potential for local resource access conflicts you must pro‐
43 vide your own pthread-based locking in the AST routine.
44
45 int dlm_ls_pthread_init(dlm_lshandle_t lockspace)
46 As dlm_pthread_init but initializes a thread for the specified
47 lockspace.
48
49 int dlm_pthread_cleanup()
50 Cleans up the default lockspace threads after use. Normally you don't
51 need to call this, but if the locking code is in a dynamically loadable
52 shared library this will probably be necessary.
53 For non-pthread based applications the DLM provides a file descriptor
54 that the program can feed into poll/select. If activity is detected on
55 that FD then a dispatch function should be called:
56
57 int dlm_get_fd()
58 Returns a file-descriptor for the DLM suitable for passing in to poll()
59 or select().
60
61 int dlm_dispatch(int fd)
62 Reads from the DLM and calls any AST routines that may be needed. This
63 routine runs in the context of the caller so no extra locking is needed
64 to protect local resources.
65
67 There also exists a "light" version of the libdlm library called lib‐
68 dlm_lt. This is provided for those applications that do not want to use
69 pthread functions. If you use this library it is important that your
70 application is NOT compiled with -D_REENTRANT or linked with
71 libpthread.
72
73
75 Create a lockspace and start a thread to deliver its callbacks:
76 dlm_lshandle_t ls;
77
78 ls = dlm_create_lockspace("myLS", 0660);
79 dlm_ls_pthread_init(ls);
80
81 ...
82
83 status = dlm_ls_lock(ls,
84 ... );
85
86
87
88 Using poll(2) to wait for and dispatch ASTs
89
90
91 static int poll_for_ast(dlm_lshandle_t ls)
92 {
93 struct pollfd pfd;
94
95 pfd.fd = dlm_ls_get_fd(ls);
96 pfd.events = POLLIN;
97 while (!ast_called)
98 {
99 if (poll(&pfd, 1, 0) < 0)
100 {
101 perror("poll");
102 return -1;
103 }
104 dlm_dispatch(dlm_ls_get_fd(ls));
105 }
106 ast_called = 0;
107 return 0;
108 }
109
110
111
113 libdlm(3), dlm_lock(3), dlm_unlock(3), dlm_open_lockspace(3), dlm_cre‐
114 ate_lockspace(3), dlm_close_lockspace(3), dlm_release_lockspace(3)
115
116
117
118libdlm functions July 5, 2007 LIBDLM(3)