Switch to unified view

a b/src/utils/ptmutex.cpp
1
/* Copyright (C) 2004 J.F.Dockes
2
 *   This program is free software; you can redistribute it and/or modify
3
 *   it under the terms of the GNU General Public License as published by
4
 *   the Free Software Foundation; either version 2 of the License, or
5
 *   (at your option) any later version.
6
 *
7
 *   This program is distributed in the hope that it will be useful,
8
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *   GNU General Public License for more details.
11
 *
12
 *   You should have received a copy of the GNU General Public License
13
 *   along with this program; if not, write to the
14
 *   Free Software Foundation, Inc.,
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
18
//
19
// Small test program to evaluate the cost of using mutex locks: calls
20
// to methods doing a small (150 bytes) base64 encoding job + string
21
// manips, with and without locking.  The performance cost is
22
// negligible on all machines I tested (around 0.3% to 2% depending on
23
// the system and machine), but not inexistent, you would not want
24
// this in a tight loop.
25
26
#include <stdio.h>
27
#include <stdlib.h>
28
#include <unistd.h>
29
30
#include <string>
31
using namespace std;
32
33
#include "ptmutex.h"
34
#include "base64.h"
35
36
static char *thisprog;
37
static char usage [] =
38
"ptmutex [-l] count\n"
39
"\n"
40
;
41
static void
42
Usage(void)
43
{
44
    fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
45
    exit(1);
46
}
47
48
static int     op_flags;
49
#define OPT_MOINS 0x1
50
#define OPT_l   0x2 
51
52
static const string convertbuffer = 
53
    "* The recoll GUI program sometimes crashes when running a query while\
54
       the indexing thread is active. Possible workarounds:";
55
56
static PTMutexInit o_lock;
57
void workerlock(string& out)
58
{
59
    PTMutexLocker locker(o_lock);
60
    base64_encode(convertbuffer, out);
61
}
62
63
void workernolock(string& out)
64
{
65
    base64_encode(convertbuffer, out);
66
}
67
68
int main(int argc, char **argv)
69
{
70
    int count = 0;
71
    thisprog = argv[0];
72
    argc--; argv++;
73
74
    while (argc > 0 && **argv == '-') {
75
  (*argv)++;
76
  if (!(**argv))
77
      /* Cas du "adb - core" */
78
      Usage();
79
  while (**argv)
80
      switch (*(*argv)++) {
81
      case 'l':   op_flags |= OPT_l; break;
82
      default: Usage();   break;
83
      }
84
    b1: argc--; argv++;
85
    }
86
87
    if (argc != 1)
88
  Usage();
89
    count = atoi(*argv++);argc--;
90
91
    if (op_flags & OPT_l) {
92
  fprintf(stderr, "Looping %d, locking\n", count);
93
  for (int i = 0; i < count; i++) {
94
      string s;
95
      workerlock(s);
96
  }
97
    } else {
98
  fprintf(stderr, "Looping %d, no locking\n", count);
99
  for (int i = 0; i < count; i++) {
100
      string s;
101
      workernolock(s);
102
  }
103
    }
104
    exit(0);
105
}
106