~ubuntu-branches/ubuntu/warty/ncbi-tools6/warty

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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
/*   ncbiprop.c
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE                          
*               National Center for Biotechnology Information
*                                                                          
*  This software/database is a "United States Government Work" under the   
*  terms of the United States Copyright Act.  It was written as part of    
*  the author's official duties as a United States Government employee and 
*  thus cannot be copyrighted.  This software/database is freely available 
*  to the public for use. The National Library of Medicine and the U.S.    
*  Government have not placed any restriction on its use or reproduction.  
*                                                                          
*  Although all reasonable efforts have been taken to ensure the accuracy  
*  and reliability of the software and data, the NLM and the U.S.          
*  Government do not and cannot warrant the performance or results that    
*  may be obtained by using this software or data. The NLM and the U.S.    
*  Government disclaim all warranties, express or implied, including       
*  warranties of performance, merchantability or fitness for any particular
*  purpose.                                                                
*                                                                          
*  Please cite the author in any work or product based on this material.   
*
* ===========================================================================
*
* File Name:  ncbiprop.c
*
* Author:  Schuler
*
* Version Creation Date:   06-04-93
*
* $Revision: 6.3 $
*
* File Description:		Application Property Functions.
*
*		Application properties were	introduced to allow the NCBI Toolbox 
*		to be implemented as a dynamic link library (DLL).  Under Windows, 
*		global variables located in the DLL are not instantiated for each 
*		application	instance, but instead shared by all applications having 
*		a run-time linkage.  Values that might ordinarily be stored in 
*		global variables can instead be saved as application properties
*		(unless sharing is desired, of course).  An application property
*		is simply a pointer (or other value cast to a pointer) that is
*		identified by a string key.  Properties are kept in a linked list
*		(sorted by key) whose head is stored in an application context block.
*		The application context block is created on-demand and marked 
*		with the process ID of the calling application (or it's creation
*		can be forced by calling InitAppContext() at the beginning of your
*		program.  The linked list of (smallish) application context 
*		structures is the only thing allocated from the DLL's data space, 
*		all other memory is owned by the application that called the DLL 
*		is automatically freed by the system when the application terminates.
*		
*		If this code is not actually compiled as a DLL, but bound at link
*		time to a single application in the normal way, the behavior of all
*		functions will be the same.  The only difference being that the
*		list of application context structures will contain exactly one item.
*
* Modifications:  
* --------------------------------------------------------------------------
* $Log: ncbiprop.c,v $
* Revision 6.3  2001/03/02 19:52:34  vakatov
* Do not use "pid" in the app.context anymore.
* It was needed for 16-bit MS-Win DLLs, a long time ago, and now it's
* just eating resources...
*
* Revision 6.2  1998/08/24 17:42:02  kans
* fixed old style function definition warnings
*
* Revision 6.1  1997/10/29 02:44:20  vakatov
* Type castings to pass through the C++ compiler
*
* Revision 6.0  1997/08/25 18:16:56  madden
* Revision changed to 6.0
*
* Revision 5.6  1997/01/28 21:19:12  kans
* <Desk.h>, <OSEvents.h> and <GestaltEqu.h> are obsolete in CodeWarrior
*
 * Revision 5.5  1997/01/08  22:55:36  shavirin
 * Added threads comparison through NlmThreadCompare
 *
 * Revision 5.4  1996/12/03  21:48:33  vakatov
 * Adopted for 32-bit MS-Windows DLLs
 *
 * Revision 5.3  1996/11/25  19:05:30  vakatov
 * Made Get/ReleaseAppContext() MT-safe
 *
 * Revision 5.2  1996/07/16  19:58:01  vakatov
 * Nlm_GetScratchBuffer() now has a "size" parameter and has a variable
 * size -- thus it works as an fast and smart "ad hoc" memory allocator.
 * Added "corelib.h" header;  thread_id argum. added to new_AppContext(...);
 * delete_AppContext(): new cleaning funcs ReleaseApp{Err|Msg}Info called.
 * Added a code to test error posting functions(they use the ScratchBuffer
 * and Properties intensively) for multiple concurrent threads.
 *
 * Revision 5.1  1996/06/12  20:18:29  shavirin
 * Added multy-thread ability of toolkit to handle error posting
* ==========================================================================
*/

#include "corepriv.h"



/*	----------------------------------------------------------------------
 *	new_AppProperty			allocates/constructs an AppProperty struct
 *	delete_AppProperty		deallocates/destructs an AppProperty struct
 *
 *	Created: 
 *		06-04-93 Schuler
 *	Modified:
 *		00-00-00 YourName	What changes did you make?
 *	----------------------------------------------------------------------	*/

typedef struct _AppProperty_
{
	struct _AppProperty_	*next;
	char *key;
	void *data;
}
AppProperty;

static AppProperty * new_AppProperty PROTO((const char *key, void *data));
static void delete_AppProperty PROTO((AppProperty *prop));

static AppProperty * new_AppProperty (const char *key, void *data)
{
  AppProperty *prop = (AppProperty *)Calloc(1, sizeof(AppProperty));
  ASSERT_HARD(key != NULL);
  ASSERT_HARD(*key != '\0');
	
  if (prop != NULL)
    {
      if ( (prop->key = StrSave(key)) == NULL )
        {
          Free(prop);
          prop = NULL;
        }
      else
        prop->data = data;
    }

  return prop;
}


static void delete_AppProperty (AppProperty *prop)
{
	ASSERT_HARD(prop != NULL);
	MemFree(prop->key);
        MemFill(prop, '\xFF', sizeof(*prop));
	Free(prop);
}



/*	----------------------------------------------------------------------
 *	new_AppContext			allocates/constructs an AppContext struct
 *	delete_AppContext		deallocates/destructs an AppContext struct
 *
 *  Notes:  (1) If a valid context cannot be created, the application halts.
 *	    (2) The memory allocated for the AppContext struct is owned by
 *		the DLL and the scratch buffer is owned by the application.
 *	    (3) It is important that this function NOT call TRACE, Message,
 *		or ErrPost (or anything else that could result in any of these
 *		being called) as they use the applicaion context's scratch buffer.
 *         
 *          For every non-thread capable platform tid == 0
 *
 *	Created: 
 *		06-04-93 Schuler
 *	Modified:
 *		00-00-00 YourName	What changes did you make?
                06-12-96 Shavirin       Added TNlmThread tid element to
                                        distinguish contexes belong to diffrent threads
 *	----------------------------------------------------------------------	*/

/*
#define SCRATCH_SIZE_DEFAULT	(2*KBYTE)
*/

typedef struct _AppContext_
{
	struct _AppContext_ 	*next;
	struct _AppProperty_	*proplist;
   TNlmThread tid;

	unsigned	enums	:15; /* number of nested enumerations in-progress */
	unsigned	lock	:1;  /* if TRUE, property list is locked */
	size_t	scratch_size;
	void	*scratch;
}
AppContext;

		/* this is the only global variable in this file: */
static AppContext * g_appList;	/* Application Context List */

static AppContext * new_AppContext(TNlmThread thread_id);
static void delete_AppContext PROTO((AppContext *prop));
INLINE static void  AppContext_Lock PROTO((AppContext *context));
INLINE static void  AppContext_Unlock PROTO((AppContext *context));
INLINE static unsigned  AppContext_IsLocked PROTO((AppContext *context));


static AppContext * new_AppContext(TNlmThread thread_id)
{
	AppContext *context = (AppContext *)dll_Malloc(sizeof(AppContext));
	
	if (context == NULL)
		AbnormalExit(1);
		
	memset((void*)context,0,sizeof(struct _AppContext_));
   context->tid = thread_id; /* thread id for thread-capable OS */
	context->scratch_size = 0;
	context->scratch = NULL;
	return context;
}

static void delete_AppContext (AppContext *context)
{
  AppProperty *p1, *p2;
	
  ASSERT_HARD(context != NULL);
	
  ReleaseAppErrInfo();
  ReleaseAppMsgInfo();

  for (p1=context->proplist; p1; p1=p2)
    {
      p2 = p1->next;
      delete_AppProperty(p1);
    }

  Nlm_GetScratchBuffer( 0 );
  dll_Free(context);
}

static INLINE void AppContext_Lock (AppContext *context)
{
	ASSERT(context->lock==0);
	context->lock = 1;
}

static INLINE void AppContext_Unlock (AppContext *context)
{
	ASSERT(context->lock==1);
	context->lock = 0;
}

static INLINE unsigned AppContext_IsLocked (AppContext *context)
{
	return context->lock;
}



/*	----------------------------------------------------------------------
 *	InitAppContext -- Initializes a context struct for current application.
 *
 *  Notes:  
 *		If a valid context cannot be created, the application halts. 
 *		Although it is not strictly necessary to call InitAppContext() as
 *		contexts are created on-demand, calling it once at the beginning
 *		of the program may reduce heap fragmentation.
 *
 *	Created: 
 *		06-04-93 Schuler
 *	Modified:
 *              06-12-96 Shavirin   Added multy-thread ability of context handling
 *                                 
 *
 *	----------------------------------------------------------------------	*/

/* helper functions for internal use only */

static AppContext *GetAppContext (void)
{
  AppContext *p1, *p2;
  AppContext *app;
  TNlmThread thread_id = NlmThreadSelf(); /* thread ID */
	
  NlmMutexLockEx( &corelibMutex );

  /*
   *	First we scan the list of contexts for one with the current
   *	application's process ID.
   */
  for (p1=g_appList,p2=NULL; p1; p1=p1->next)
    {
      if ( NlmThreadCompare(p1->tid, thread_id) )
        {
          NlmMutexUnlock( corelibMutex );
          return p1;
        }
      p2 = p1;
    }
	
  /* 
   *	If we reach this point, the context for current does not 
   *	exist yet, so we need to create one and link it into the list.
   */
  app = new_AppContext(thread_id);

  if (p2 == NULL)	 
    g_appList = app;
  else  
    p2->next = app;	
  app->next = p1;

  NlmMutexUnlock( corelibMutex );

  return app;
}


NLM_EXTERN void LIBCALL Nlm_InitAppContext (void)
{
  GetAppContext ();
}


NLM_EXTERN char * LIBCALL Nlm_GetScratchBuffer(size_t size)
{
  AppContext *context = GetAppContext();
  if (context == NULL)
    abort();

  if ((size == 0  ||  context->scratch_size < size)  &&
      context->scratch != NULL)
    {
      /* reset scratch buffer */
      Free( context->scratch );
      context->scratch_size = 0;
      context->scratch = NULL;
    }

  /* nothing but reset */
  if (size == 0)
    return NULL;

  /* do we have enough allocated memory already? */
  if (context->scratch_size >= size)
    return (char *) context->scratch;

  /* allocate new buffer */
  size = (size + 15) / 16;
  size *= 16;
  if ((context->scratch = (char *) Malloc( size )) == NULL)
    return NULL;

  context->scratch_size = size;
  return (char *) context->scratch;
}



/*	----------------------------------------------------------------------
 *	ReleaseAppContext -- frees application context struct for current app.
 *
 *	Notes:
 *		For most platforms, memory will be recovered automatically by the
 *		operating system.  However, since we cannot guarantee this for
 *		all systems, it might be wise to call ReleaseAppContext() once
 *		just before the application exits.
 *
 *	Created: 
 *		06-04-93 Schuler
 *	Modified:
 *              06-12-96 Shavirin Added multi-thread ability of context handling
 *                                
 *
 *	----------------------------------------------------------------------	*/

NLM_EXTERN void LIBCALL Nlm_ReleaseAppContext (void)
{
  AppContext *p1, *p2;
  TNlmThread thread_id = NlmThreadSelf();

  NlmMutexLockEx( &corelibMutex );

  /*
   *	Scan the list for the context of the current app
   */
  for (p1=g_appList,p2=NULL; p1; p1=p1->next)
    {
      if ( NlmThreadCompare(p1->tid, thread_id) )
        break;
      p2 = p1;
    }
  /*
   *	Adjust links and release memory 
   */
  if (p1 != NULL)
    {
      AppContext *next = p1->next;
      delete_AppContext( p1 );

      if (p2 == NULL)
        g_appList = next;
      else
        p2->next  = next;
    }

  NlmMutexUnlock( corelibMutex );
}



/*	----------------------------------------------------------------------
 *	SetAppProperty -- Sets a data item int the application context's 
 *		property list, replacing the existing one or creating a new
 *		one if no property with that key exists.
 *
 *	Parameters:
 *		key:		key identifying the property
 *		value:		pointer to arbitrary data
 *
 *	Return value:	
 *		Previous value of the property, if any, or NULL otherwise.
 *
 *	Created: 
 *		06-08-93	Schuler
 *	Modified:
 *		00-00-00	YourName	What changes did you make?
 *	----------------------------------------------------------------------	*/

NLM_EXTERN void * LIBCALL Nlm_SetAppProperty (const char *key, void *data)
{
	if (key && *key)
	{
		AppContext *context = GetAppContext();
		AppProperty *p1, *p2, *prop;
		void *prev;
		int d;
		
		for (p1=context->proplist, p2=NULL; p1; p1=p1->next)
		{
			d = strcmp(key,p1->key);
			if (d < 0)  break;
			if (d==0)
			{
				prev = p1->data;
				p1->data = data;
				return prev;	/* previous value */
			}
			p2 = p1;
		}
		
		/*
		 *	If we reach here, a property with the given key does not exist, so 
		 *	let's create a new one and link it into the list.
		 */
		
		if (AppContext_IsLocked(context))
		{
			TRACE("SetAppProperty:  ** property list is locked **\n");
		}
		else
		{		
			AppContext_Lock(context);
			if ((prop = new_AppProperty(key,data)) != NULL)
			{
				if (p2 == NULL)	 
					context->proplist = prop;
				else  
					p2->next = prop;	
				prop->next = p1;
			}
			AppContext_Unlock(context);
		}
	}	
	return NULL;	/* no previous value */
}



/*	----------------------------------------------------------------------
 *	GetAppProperty -- Retrieves data value that was set with SetAppProperty.
 *
 *	Parameters:
 *		key:	key identifying the property
 *
 *	Return value:	
 *		Value that was set with SetAppProperty or NULL if no property with 
 *		that key exists.
 *
 *	Created: 
 *		06-08-93	Schuler
 *	Modified:
 *		00-00-00	YourName	What changes did you make?
 *
 *	----------------------------------------------------------------------	*/

NLM_EXTERN void * LIBCALL Nlm_GetAppProperty (const char *key)
{
	if (key && *key)
	{
		AppContext *context = GetAppContext();
		AppProperty *prop;
		
		for (prop=context->proplist; prop; prop=prop->next)
		{
			if (strcmp(prop->key,key) == 0)
				return prop->data;
		}
	}
	return NULL;
}



/*	----------------------------------------------------------------------
 *	RemoveAppProperty -- Removes a property from the application context's
 *		property list (if it exists) and returns the data value that was
 *		set with SetAppParam().
 *
 *	Parameters:
 *		key:	key identifying the property
 *
 *	Return value:	
 *		Value that was set with SetAppProperty or NULL if no property with 
 *		that key exists.
 *
 *	Notes:
 *		It is the responsibiliy of the caller to free whatever resources 
 *		the property's data (return value) may happen to point to.
 *
 *	Created: 
 *		06-08-93	Schuler
 *	Modified:
 *		00-00-00	YourName	What changes did you make?
 *
 *	----------------------------------------------------------------------	*/

NLM_EXTERN void * LIBCALL Nlm_RemoveAppProperty (const char *key)
{
	if (key && *key)
	{
		AppContext *context = GetAppContext();
		
		if (AppContext_IsLocked(context))
		{
			TRACE("RemoveAppProperty:  ** property list is locked **\n");
		}
		else
		{
			AppProperty *p1, *p2;
			int d;
			
			AppContext_Lock(context);
			for (p1=context->proplist, p2=NULL; p1; p1=p1->next)
			{
				d = strcmp(key,p1->key);
				if (d < 0)  break;
				if (d==0)
				{
					void *data = p1->data;
					if (p2 == NULL)
						context->proplist = p1->next;
					else
						p2->next = p1->next;
					delete_AppProperty(p1);
					AppContext_Unlock(context);
					return data;	/* success */
				}
				p2 = p1;
			}
			AppContext_Unlock(context);
		}
	}
	return NULL;	/* failure */		
}



/*	----------------------------------------------------------------------
 *	EnumAppProperties -- Enumerates all application properties, calling
 *		a caller-supplied callback function with the key and data for
 *		each one.
 *
 *	Parameters:
 *		Pointer to the enumeration callback procedure.
 *
 *	Return value:
 *		Number of properties enumerated.
 *
 *	Callback function:
 *
 *		int LIBCALLBACK MyEmumProc (const char *key, void *value)
 *		{
 *			//--- insert your code here ---
 *
 *			// for example:
 *			if (strcmp(key,"MyBigBuffer") ==0)
 *			{
 *				SetAppProperty(key,NULL);
 *				MemFree(data)
 *				return FALSE;	// FALSE to stop enumeration at this point
 *			}
 *			return TRUE;		// TRUE to continue the enumeration.
 *		}
 *
 *	Notes:
 *		It is OK to call SetAppProperty() from within the callback function,
 *		but _only_ to change the value of an existing property.  Any attempt 
 *		to alter the property list, either by calling SetAppProperty() with 
 *		a new key or calling RemoveAppProperty() will fail while an enumera-
 *		tion is in progress.
 *
 *	Created: 
 *		06-09-93	Schuler
 *	Modified:
 *		00-00-00	YourName	What changes did you make?
 *
 *	----------------------------------------------------------------------	*/

NLM_EXTERN int LIBCALL Nlm_EnumAppProperties (Nlm_AppPropEnumProc proc)
{
	int count = 0;
	if (proc != NULL)
	{
		AppContext *context = GetAppContext();
		AppProperty *prop;
		
		if (context->enums==0)
			AppContext_Lock(context);
		context->enums++;
		for (prop=context->proplist; prop; prop=prop->next)
		{
			count ++;
			if ( ! (*proc)(prop->key,prop->data) )
				break;
		}
		context->enums--;
		if (context->enums==0)
			AppContext_Unlock(context);
	}
	return count;
}


/*	----------------------------------------------------------------------
 *	GetAppProcessID  [Schuler, 06-04-93]
 *
 *	Returns an identifier for the current application instance.
 *
 *	Notes:
 *	On the Macintosh, the process ID is a 64-bit value, which is being
 *	condensed down to 32-bits here by XORing the high and low halves
 *	of the value.  I can't guarantee it will be unique (although it
 *	seems to be in practice), but this code is not being dynamically
 *	linked on the Mac, so it doesn't matter.
 *
 *	MODIFICATIONS
 *	04-10-93  Schuler  Added Macintosh version.
 *	12-16-93  Schuler  Added Borland version contributed by M.Copperwhite
 *	----------------------------------------------------------------------	*/

#define USE_GETPID

/* (insert other platform-specific versions here as necessary and #undef USE_GETPID) */


/* ----- Macintosh Version ----- */
#ifdef OS_MAC
#include <Processes.h>
#include <Gestalt.h>

NLM_EXTERN long LIBCALL Nlm_GetAppProcessID (void)
{
	long gval;
	ProcessSerialNumber psn;	/* a 64-bit value*/
	
    if (Gestalt (gestaltSystemVersion, &gval) == noErr && (short) gval >= 7 * 256) {
		GetCurrentProcess(&psn);	
		return (psn.highLongOfPSN ^ psn.lowLongOfPSN);	/* merge to 32-bits */
	} else {
		return 1;
	}
}

#undef USE_GETPID
#endif


/* ----- NCBIDLL mod Borland DLL version - call Windows API to get PSP ----- */
#ifdef _WINDLL
#ifdef COMP_BOR

NLM_EXTERN long LIBCALL Nlm_GetAppProcessID (void)
{
  return GetCurrentPDB();
}

#undef USE_GETPID
#endif
#endif


/* ----- Generic Version ----- */
#ifdef USE_GETPID
#if defined(COMP_MSC) || defined(COMP_BOR)
#include <process.h>
#endif

NLM_EXTERN long LIBCALL Nlm_GetAppProcessID (void)
{
	return getpid();
}

#endif



#ifdef TEST_MODULE_NCBIPROP

/***********************************************************************
 *  TEST
 ***********************************************************************/

#include <stdio.h>
#include <ncbistd.h>
#include <tsprintf.h>

FILE *STDOUT = (FILE *)stdout;
FILE *STDERR = (FILE *)stderr;

static Nlm_VoidPtr TEST__MyThread(Nlm_VoidPtr arg)
{
  Nlm_Int4 thread_no = (Nlm_Int4) arg;
  Nlm_Int4 n, i;
  Nlm_CharPtr str = NULL;
  const Nlm_Char PNTR s = NULL;
  
  fprintf(STDOUT, "TEST__MyThread():  Thread #%ld(%ld) started\n",
          (long)NlmThreadSelf(), (long)thread_no);

  n = (thread_no + 1) * 20;
  str = (Nlm_CharPtr)Nlm_MemNew( n );
  for (i = 10;  i < n;  i += 10)
    {
      Nlm_MemSet(str, 'a', i);
      str[i] = '\0';
      s = TSPrintf("TEST__MyThread() #%ld: %u %d %e %g %f %p %s +++\n",
                   (long)thread_no, (unsigned int)i, (int)i, (double)i,
                   (double)i, (double)i, (void *)&i, str);
      fprintf(STDOUT, "%p  %ld: %s\n", (void *)s, Nlm_StrLen( s ), s);
    }

  Nlm_MemFree( str );

#if defined (OS_UNIX)
  sleep(thread_no + 1);
#elif defined (WIN32)
  Sleep(1000 * (thread_no + 1));
#endif

  
  fprintf(STDOUT, "TEST__MyThread() after sleep #%ld: %p  %ld: %s\n",
          (long)thread_no, (void *)s, Nlm_StrLen( s ), s);

  switch (thread_no % 4)
    {
    case 0:
      Nlm_Message(MSG_OKC,
                  "TEST__MyThread() #%ld: Nlm_Message(MSG_OKC)\n",
                  (long)thread_no);
      break;

    case 1:
      Nlm_MsgAlert (MSG_YN,  MSG_POST,
                    "Nlm_MsgAlert(): Caption\n",
                    "TEST__MyThread() #%ld: Nlm_MsgAlert(MSG_YN,MSG_POST)\n",
                    (long)thread_no);
      break;

    case 2:
      Nlm_ErrPost (CTX_DEBUG/*CTX_UNKNOWN*/,  1,
                    "TEST__MyThread() #%ld: Nlm_ErrPost(CTX_DEBUG,1)\n",
                    (long)thread_no);
      break;

    case 3:
      Nlm_ErrPostEx (SEV_ERROR, 1, 2,
                    "TEST__MyThread() #%ld: Nlm_ErrPostEx(0,1,2)\n",
                    (long)thread_no);
      break;
    }

  return NULL;
}


static void MyExitMessage(Nlm_VoidPtr arg)
{
  fprintf(STDERR, "\n%s\n", (Nlm_CharPtr)arg);
  Nlm_MemFree( arg ); 
}

static Nlm_Int4 TEST__scratch(Nlm_Int4 n_threads)
{
  Nlm_Int4 t;
  Nlm_Int4 err_code = 0;
  TNlmThread *threads = Nlm_MemNew(n_threads * sizeof(TNlmThread));

  for (t = 0;  t < n_threads;  t++)
    {
      switch (t % 2)
        {
        case 0:
          threads[t] = NlmThreadCreate(TEST__MyThread, (Nlm_VoidPtr)t,
                                       THR_RUN);
          break;
        case 1:
          {
            Nlm_CharPtr exit_message = Nlm_MemNew( 64 );
            sprintf(exit_message, "Exit Message, thread #%d", (int)t);
            threads[t] = NlmThreadCreateEx(
              TEST__MyThread, (Nlm_VoidPtr)t, THR_RUN,
              MyExitMessage, (Nlm_VoidPtr)exit_message);
            break;
          }
        }
      fprintf(STDOUT, "TEST__scratch():  Starting thread #%ld\n",
              (long)threads[t]);
    }

  for (t = 0;  t < n_threads;  t++)
    {
      Nlm_VoidPtr status;
      err_code += NlmThreadJoin(threads[t], &status);
      if (err_code != 0)
        {
          fprintf(STDOUT, "TEST__scratch():  Cannot join thread #%ld;  \
error code = %ld\n",
                  (long)threads[t], (long)err_code);
          break;
        }

      fprintf(STDOUT, "TEST__scratch():  Thread #%ld joined;  \
terminated, exit status = %p\n",
              (long)threads[t], status);
    }

  Nlm_MemFree( threads );  

  fprintf(STDOUT, "TEST__scratch():  FINISHED\n");
  return err_code;
}


Nlm_Int2 Nlm_Main( void )
{
#if defined(WIN32) && defined(_WINDOWS)
  (FILE *)STDOUT = freopen("stdout.w95", "w", stdout);
  (FILE *)STDERR = freopen("stderr.w95", "w", stderr);
  if (!STDOUT  ||  !STDERR)
    abort();
#endif
      
  return (Nlm_Int2)TEST__scratch( 10 );
}

#endif  /* TEST_MODULE_NCBIPROP */