File: c-app6.htm

package info (click to toggle)
aolserver 3.4.2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 22,692 kB
  • ctags: 33,612
  • sloc: ansic: 171,340; tcl: 10,218; sh: 3,821; cpp: 2,779; makefile: 2,041; yacc: 1,648; perl: 456; php: 13
file content (190 lines) | stat: -rw-r--r-- 9,142 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<HTML><HEAD>
<TITLE>C Examples -- Example 6: tclcs</TITLE>
<LINK rel=Previous href="c-app5.htm">
<LINK rel=ToC href="toc.htm">
<LINK rel=Index href="master.htm">
<LINK rel=Next href="c-app7.htm">
</HEAD><BODY BGCOLOR="#ffffff"><A NAME="topofpage"></A>
<TABLE WIDTH=100%>
  <TR>
    <TD ALIGN=LEFT>
      <A NAME="topofpage"></A> <IMG  SRC="as-c-sm.gif">
    </TD>
    <TD ALIGN=RIGHT>
      <A href="c-app5.htm"><IMG  BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
      <A href=toc.htm> <IMG  BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
      <A href=master.htm> <IMG  BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
      <A href="c-app7.htm"> <IMG  BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
      <A name="7983"> </A>
    </TD>
  </TR>
</TABLE>

<a name="16870">
</a><h3>Example 6: tclcs</h3>
<p><a name="16889">
</a>The following example adds critical section <code>enter</code> and <code>leave</code> commands to Tcl. This allows you to protect shared resources (i.e., an external file or remote connection) accessed in a Tcl script.</p>
<p><a name="16887">
</a>This example can be found in the <code>examples/c/tclcs</code> directory.</p>
<pre>    <a name="16866"></a>
    <a name="16901"></a>#include "ns.h"
    <a name="16906"></a>#include "nstcl.h"
    <a name="16907"></a>
    <a name="16908"></a>/*
    <a name="16909"></a> * This code implements simple critical section primitives as TCL commands 
    <a name="16910"></a> * within the AOLserver.  To use this module, add the following to the 
    <a name="16911"></a> * [ns\server\server-name\modules] section of your nsd.ini file:
    <a name="16912"></a> * cs=tclcs.dll    ; or tclcs.so on Unix platforms
    <a name="16913"></a> * 
    <a name="16914"></a> * Within your TCL, these commands should be used as follows:
    <a name="16915"></a> *      ...
    <a name="16916"></a> *      cs_enter
    <a name="16917"></a> *      set err [catch {some tcl}]
    <a name="16918"></a> *      cs_leave
    <a name="16919"></a> *      if $err {
    <a name="16920"></a> *              error $err
    <a name="16921"></a> *      }
    <a name="16922"></a> *      ...
    <a name="16924"></a> * Note that any error should be caught to avoid leaving the
    <a name="16925"></a> * critical section in the locked state after the script exits.
    <a name="16926"></a> * 
    <a name="16927"></a> * Also, because the init function uses the module name to construct
    <a name="16928"></a> * the command names,  you can load the module multiple times if you
    <a name="16929"></a> * need more than one critical section.  For example, if you needed 2
    <a name="16930"></a> * critical sections, your nsd.ini would have:
    <a name="16931"></a> *
    <a name="16932"></a> * [ns\server\server-name\modules]
    <a name="16933"></a> * cs1=tclcs.dll
    <a name="16934"></a> * cs2=tclcs.dll
    <a name="16935"></a> *
    <a name="16936"></a> * and then, in your Tcl script, you would access the two critical
    <a name="16937"></a> * sections using their unique names:
    <a name="16938"></a> *
    <a name="16939"></a> *	...
    <a name="16940"></a> *	cs1_enter
    <a name="16941"></a> *	... access resource protected by cs 1 ...
    <a name="16942"></a> * 	cs2_enter
    <a name="16943"></a> * 	... access resource 2, leaving resource 1 locked ...
    <a name="16944"></a> * 	cs2_leave
    <a name="16945"></a> * 	cs1_leave
    <a name="16946"></a> * 	
    <a name="16947"></a> */
    <a name="16948"></a>
    <a name="16949"></a>int Ns_ModuleVersion = 1;
    <a name="16950"></a>
    <a name="16951"></a>static int InitCs(Tcl_Interp *interp, void *ctx);
    <a name="16952"></a>static Tcl_CmdProc EnterCs, LeaveCs;
    <a name="16953"></a>
    <a name="16954"></a>/*
    <a name="16955"></a> * This structure is used to pass the critical section
    <a name="16956"></a> * and enter and leave command names to InitCs function
    <a name="16957"></a> * through the Ns_TclInitInterps function.
    <a name="16958"></a> */
    <a name="16959"></a>typedef struct {
    <a name="16960"></a>	char *enter;
    <a name="16961"></a>	char *leave;
    <a name="16962"></a>	Ns_CriticalSection *cs;
    <a name="16963"></a>} CsCtx;
    <a name="16964"></a>
    <a name="16965"></a>
    <a name="16966"></a>/*
    <a name="16967"></a> * Ns_ModuleInit - The function is called each time the
    <a name="16968"></a> * tclcs module is loaded into a server.  It
    <a name="16969"></a> * constructs the names of the enter and leave Tcl commands
    <a name="16970"></a> * from the module name and then calls Ns_TclInitInterps
    <a name="16971"></a> * to add the command to each interpreter of the 
    <a name="16972"></a> * server.
    <a name="16973"></a> */
    <a name="16974"></a>int
    <a name="16975"></a>Ns_ModuleInit(char *hServer, char *hModule)
    <a name="16976"></a>{
    <a name="16977"></a>	Ns_DString dsEnter;
    <a name="16978"></a>	Ns_DString dsLeave;
    <a name="16979"></a>	CsCtx ctx;
    <a name="16980"></a>	int status;
    <a name="16981"></a>
    <a name="16982"></a>	Ns_DStringInit(&amp;dsEnter);
    <a name="16983"></a>	Ns_DStringAppend(&amp;dsEnter, hModule);
    <a name="16984"></a>	Ns_DStringAppend(&amp;dsEnter, "_enter");
    <a name="16985"></a>
    <a name="16986"></a>	Ns_DStringInit(&amp;dsLeave);
    <a name="16987"></a>	Ns_DStringAppend(&amp;dsLeave, hModule);
    <a name="16988"></a>	Ns_DStringAppend(&amp;dsLeave, "_leave");
    <a name="16989"></a>
    <a name="16990"></a>	ctx.enter = dsEnter.string;
    <a name="16991"></a>	ctx.leave = dsLeave.string;
    <a name="16992"></a>        ctx.cs = ns_malloc(sizeof(Ns_CriticalSection));
    <a name="16993"></a>        Ns_InitializeCriticalSection(ctx.cs);
    <a name="16994"></a>
    <a name="16995"></a>        status = Ns_TclInitInterps(hServer, InitCs, (void *) &amp;ctx);
    <a name="16996"></a>
    <a name="16997"></a>	Ns_DStringFree(&amp;dsEnter);
    <a name="16998"></a>	Ns_DStringFree(&amp;dsLeave);
    <a name="16999"></a>	return status;
    <a name="17000"></a>}
    <a name="17001"></a>        
    <a name="17002"></a>
    <a name="17003"></a>/*
    <a name="17004"></a> * InitCs - Initialize a single Tcl interpreter with the
    <a name="17005"></a> * critical section enter and leave commands.
    <a name="17006"></a> */
    <a name="17007"></a>static int
    <a name="17008"></a>InitCs(Tcl_Interp *interp, void *ctx)
    <a name="17009"></a>{
    <a name="17010"></a>	CsCtx *csctx;
    <a name="17011"></a>
    <a name="17012"></a>	csctx = ctx;
    <a name="17013"></a>        Tcl_CreateCommand(interp, csctx-&gt;enter, EnterCs, 
    <a name="17063"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(ClientData) csctx-&gt;cs, NULL);
    <a name="17014"></a>        Tcl_CreateCommand(interp, csctx-&gt;leave, LeaveCs, 
    <a name="17064"></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(ClientData) csctx-&gt;cs, NULL);
    <a name="17015"></a>        return TCL_OK;
    <a name="17016"></a>}
    <a name="17017"></a>
    <a name="17018"></a>
    <a name="17019"></a>/* 
    <a name="17020"></a> * EnterCs - Enter the critical section passed in as callback data.
    <a name="17021"></a> */
    <a name="17022"></a>static int
    <a name="17023"></a>EnterCs(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
    <a name="17024"></a>{
    <a name="17025"></a>        Ns_CriticalSection *cs = (Ns_CriticalSection *) clientData;
    <a name="17026"></a>
    <a name="17027"></a>        Ns_EnterCriticalSection(cs);
    <a name="17028"></a>        return TCL_OK;
    <a name="17029"></a>}
    <a name="17030"></a>
    <a name="17031"></a>
    <a name="17032"></a>/* 
    <a name="17033"></a> * LeaveCs - Leave the critical section passed in as callback data.
    <a name="17034"></a> */
    <a name="17035"></a>static int
    <a name="17036"></a>LeaveCs(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
    <a name="17037"></a>{
    <a name="17038"></a>        Ns_CriticalSection *cs = (Ns_CriticalSection *) clientData;
    <a name="17039"></a>
    <a name="17040"></a>        Ns_LeaveCriticalSection(cs);
    <a name="17041"></a>        return TCL_OK;
    <a name="17042"></a>}
</pre><p>

<TABLE BORDER="2" CELLPADDING="1" width="100%">
<TR><TD COLSPAN=3><P ALIGN=Center>
<IMG SRC="bluebult.gif">
<A HREF="#topofpage">
<FONT SIZE=-1>Top of Page</FONT></A>
<IMG SRC="bluebult.gif">
</TD></TR>
<TR><TD COLSPAN=3><P ALIGN=Center>
<A href="c-app5.htm">
<IMG  BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm>
<IMG  BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm>
<IMG  BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="c-app7.htm">
<IMG  BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<BR align=center>
<FONT size=-1>Copyright &copy; 1998-99 America Online,
Inc.</FONT>
</TD></TR></TABLE></BODY></HTML>