mirror of
https://github.com/xroche/httrack.git
synced 2026-05-17 08:26:14 +03:00
http://proliant/svn/httrack/trunk ........ r975 | roche | 2014-05-02 17:13:29 +0200 (ven., 02 mai 2014) | 1 line Big cleanup in functions writing to a char buffer without proper size boundary. ........ r977 | roche | 2014-05-02 17:16:18 +0200 (ven., 02 mai 2014) | 3 lines Add htsconcat.c ........ r978 | roche | 2014-05-02 19:44:51 +0200 (ven., 02 mai 2014) | 3 lines Fixes. ........ r979 | roche | 2014-05-02 19:53:49 +0200 (ven., 02 mai 2014) | 1 line Fixed WIN32. ........ r980 | roche | 2014-05-02 20:05:57 +0200 (ven., 02 mai 2014) | 1 line HTTRACK_DEFLIB cleanup. ........ r981 | roche | 2014-05-02 20:39:21 +0200 (ven., 02 mai 2014) | 1 line Fixed issue #42 (long query strings with accents) ........ r982 | roche | 2014-05-02 21:19:08 +0200 (ven., 02 mai 2014) | 4 lines Added "long query string" ut/ See <https://code.google.com/p/httrack/issues/detail?id=42> ........ r983 | roche | 2014-05-04 10:48:23 +0200 (dim., 04 mai 2014) | 1 line Big cleanup in string primitives and abort functions ........ r985 | roche | 2014-05-04 11:01:07 +0200 (dim., 04 mai 2014) | 3 lines Newlines. ........ r986 | roche | 2014-05-04 13:00:49 +0200 (dim., 04 mai 2014) | 3 lines Added htssafe.h ........ r987 | roche | 2014-05-04 13:01:01 +0200 (dim., 04 mai 2014) | 3 lines Use GCC's __builtin_types_compatible_p() ........ r988 | roche | 2014-05-04 13:02:43 +0200 (dim., 04 mai 2014) | 3 lines Comment. ........ r989 | roche | 2014-05-04 16:00:30 +0200 (dim., 04 mai 2014) | 1 line More hashtable stress ........ r990 | roche | 2014-05-04 16:24:27 +0200 (dim., 04 mai 2014) | 2 lines Hashtable: logging in case of troubles ........ r991 | roche | 2014-05-04 16:29:26 +0200 (dim., 04 mai 2014) | 1 line Cleanup. ........ r992 | roche | 2014-05-04 16:35:01 +0200 (dim., 04 mai 2014) | 3 lines Fix. ........ r993 | roche | 2014-05-04 16:35:22 +0200 (dim., 04 mai 2014) | 1 line Fix 2. ........ r994 | roche | 2014-05-04 16:44:17 +0200 (dim., 04 mai 2014) | 1 line Potentially unused functions. ........ r995 | roche | 2014-05-04 16:57:41 +0200 (dim., 04 mai 2014) | 1 line Fixed hidden type. ........ r996 | roche | 2014-05-04 17:16:59 +0200 (dim., 04 mai 2014) | 1 line Replaced ugly cat by snprintf ........ r997 | roche | 2014-05-04 17:17:21 +0200 (dim., 04 mai 2014) | 1 line Added strlcatbuff ........ r998 | roche | 2014-05-04 17:50:12 +0200 (dim., 04 mai 2014) | 1 line Fixed build. ........ r999 | roche | 2014-05-04 18:04:18 +0200 (dim., 04 mai 2014) | 3 lines Added -DLIBHTTRACK_EXPORTS for libhttrack ........ r1000 | roche | 2014-05-06 20:40:50 +0200 (mar., 06 mai 2014) | 1 line Fixed regression introduced in r995 over strcpybuff() reusing macro arguments multiple times, leading to troubles when using side-effects. ........ r1001 | roche | 2014-05-06 20:42:45 +0200 (mar., 06 mai 2014) | 1 line Cleanup in dirty mallocated strings ........ r1002 | roche | 2014-05-08 11:13:54 +0200 (jeu., 08 mai 2014) | 1 line Added assertion handlers to hashtables. ........ r1003 | roche | 2014-05-08 11:14:19 +0200 (jeu., 08 mai 2014) | 1 line Assertion handler ........ r1004 | roche | 2014-05-08 11:14:48 +0200 (jeu., 08 mai 2014) | 1 line Assertion handler (2) ........ r1005 | roche | 2014-05-08 11:17:27 +0200 (jeu., 08 mai 2014) | 1 line Missing NULL. ........ r1006 | roche | 2014-05-08 11:25:11 +0200 (jeu., 08 mai 2014) | 1 line Inline keyword. ........ r1007 | roche | 2014-05-09 18:43:48 +0200 (ven., 09 mai 2014) | 3 lines assert cleanup. ........ r1008 | roche | 2014-05-09 18:52:29 +0200 (ven., 09 mai 2014) | 3 lines cache->iadr is not supposed to be NULL, but ... (could explain crashes on Android at data.app_lib.com_httrack_android_1.libhttrack_so.0x62e4c) ........ r1009 | roche | 2014-05-10 16:15:22 +0200 (sam., 10 mai 2014) | 2 lines Mitigate stash overflow by resizing the hashtable. Added better logging. ........ r1010 | roche | 2014-05-10 21:07:51 +0200 (sam., 10 mai 2014) | 1 line Hashtable: names for debugging, and better logging ........ r1011 | roche | 2014-05-10 21:08:05 +0200 (sam., 10 mai 2014) | 1 line Better logging for hashtables ........ r1012 | roche | 2014-05-11 10:27:09 +0200 (dim., 11 mai 2014) | 1 line Fixed warning: "warning: no previous prototype for 'htsthread_wait'" ........ r1013 | roche | 2014-05-12 20:54:27 +0200 (lun., 12 mai 2014) | 3 lines Fixed buggy , in man ........ r1014 | roche | 2014-05-13 21:21:12 +0200 (mar., 13 mai 2014) | 1 line 3.48.6 ........ r1016 | roche | 2014-05-13 21:24:40 +0200 (mar., 13 mai 2014) | 3 lines 3.48.6 ........
218 lines
5.2 KiB
C
218 lines
5.2 KiB
C
/* ------------------------------------------------------------ */
|
|
/*
|
|
HTTrack Website Copier, Offline Browser for Windows and Unix
|
|
Copyright (C) 1998-2014 Xavier Roche and other contributors
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
Important notes:
|
|
|
|
- We hereby ask people using this source NOT to use it in purpose of grabbing
|
|
emails addresses, or collecting any other private information on persons.
|
|
This would disgrace our work, and spoil the many hours we spent on it.
|
|
|
|
Please visit our Website: http://www.httrack.com
|
|
*/
|
|
|
|
/* ------------------------------------------------------------ */
|
|
/* File: Threads */
|
|
/* Author: Xavier Roche */
|
|
/* ------------------------------------------------------------ */
|
|
|
|
/* Internal engine bytecode */
|
|
#define HTS_INTERNAL_BYTECODE
|
|
|
|
#include "htsglobal.h"
|
|
#include "htsbase.h"
|
|
#include "htsthread.h"
|
|
#include "httrack-library.h"
|
|
|
|
#if USE_BEGINTHREAD
|
|
#ifdef _WIN32
|
|
#include <process.h>
|
|
#endif
|
|
#endif
|
|
|
|
static int process_chain = 0;
|
|
static htsmutex process_chain_mutex = HTSMUTEX_INIT;
|
|
|
|
HTSEXT_API void htsthread_wait(void) {
|
|
htsthread_wait_n(0);
|
|
}
|
|
|
|
HTSEXT_API void htsthread_wait_n(int n_wait) {
|
|
#if USE_BEGINTHREAD
|
|
int wait = 0;
|
|
|
|
do {
|
|
hts_mutexlock(&process_chain_mutex);
|
|
wait = (process_chain > n_wait);
|
|
hts_mutexrelease(&process_chain_mutex);
|
|
if (wait)
|
|
Sleep(100);
|
|
} while(wait);
|
|
#endif
|
|
}
|
|
|
|
/* ensure initialized */
|
|
HTSEXT_API void htsthread_init(void) {
|
|
#if USE_BEGINTHREAD
|
|
#if (defined(_DEBUG) || defined(DEBUG))
|
|
assertf(process_chain == 0);
|
|
#endif
|
|
if (process_chain_mutex == HTSMUTEX_INIT) {
|
|
hts_mutexinit(&process_chain_mutex);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
HTSEXT_API void htsthread_uninit(void) {
|
|
htsthread_wait();
|
|
#if USE_BEGINTHREAD
|
|
hts_mutexfree(&process_chain_mutex);
|
|
#endif
|
|
}
|
|
|
|
typedef struct hts_thread_s {
|
|
void *arg;
|
|
void (*fun) (void *arg);
|
|
} hts_thread_s;
|
|
|
|
#ifdef _WIN32
|
|
static unsigned int __stdcall hts_entry_point(void *tharg)
|
|
#else
|
|
static void *hts_entry_point(void *tharg)
|
|
#endif
|
|
{
|
|
hts_thread_s *s_args = (hts_thread_s *) tharg;
|
|
void *const arg = s_args->arg;
|
|
void (*fun) (void *arg) = s_args->fun;
|
|
|
|
free(tharg);
|
|
|
|
hts_mutexlock(&process_chain_mutex);
|
|
process_chain++;
|
|
assertf(process_chain > 0);
|
|
hts_mutexrelease(&process_chain_mutex);
|
|
|
|
/* run */
|
|
fun(arg);
|
|
|
|
hts_mutexlock(&process_chain_mutex);
|
|
process_chain--;
|
|
assertf(process_chain >= 0);
|
|
hts_mutexrelease(&process_chain_mutex);
|
|
#ifdef _WIN32
|
|
return 0;
|
|
#else
|
|
return NULL;
|
|
#endif
|
|
}
|
|
|
|
/* create a thread */
|
|
HTSEXT_API int hts_newthread(void (*fun) (void *arg), void *arg) {
|
|
hts_thread_s *s_args = malloc(sizeof(hts_thread_s));
|
|
|
|
assertf(s_args != NULL);
|
|
s_args->arg = arg;
|
|
s_args->fun = fun;
|
|
#ifdef _WIN32
|
|
{
|
|
unsigned int idt;
|
|
HANDLE handle =
|
|
(HANDLE) _beginthreadex(NULL, 0, hts_entry_point, s_args, 0, &idt);
|
|
if (handle == 0) {
|
|
free(s_args);
|
|
return -1;
|
|
} else {
|
|
/* detach the thread from the main process so that is can be independent */
|
|
CloseHandle(handle);
|
|
}
|
|
}
|
|
#else
|
|
{
|
|
const size_t stackSize = 1024 * 1024 * 8;
|
|
pthread_attr_t attr;
|
|
pthread_t handle = 0;
|
|
int retcode;
|
|
|
|
if (pthread_attr_init(&attr) != 0
|
|
|| pthread_attr_setstacksize(&attr, stackSize) != 0
|
|
|| (retcode =
|
|
pthread_create(&handle, &attr, hts_entry_point, s_args)) != 0) {
|
|
free(s_args);
|
|
return -1;
|
|
} else {
|
|
/* detach the thread from the main process so that is can be independent */
|
|
pthread_detach(handle);
|
|
pthread_attr_destroy(&attr);
|
|
}
|
|
}
|
|
#endif
|
|
return 0;
|
|
}
|
|
|
|
#if USE_BEGINTHREAD
|
|
|
|
/* Note: new 3.41 cleaned up functions. */
|
|
|
|
HTSEXT_API void hts_mutexinit(htsmutex * mutex) {
|
|
htsmutex_s *smutex = malloct(sizeof(htsmutex_s));
|
|
|
|
#ifdef _WIN32
|
|
smutex->handle = CreateMutex(NULL, FALSE, NULL);
|
|
#else
|
|
pthread_mutex_init(&smutex->handle, 0);
|
|
#endif
|
|
*mutex = smutex;
|
|
}
|
|
|
|
HTSEXT_API void hts_mutexfree(htsmutex * mutex) {
|
|
if (mutex != NULL && *mutex != NULL) {
|
|
#ifdef _WIN32
|
|
CloseHandle((*mutex)->handle);
|
|
#else
|
|
pthread_mutex_destroy(&((*mutex)->handle));
|
|
#endif
|
|
freet(*mutex);
|
|
*mutex = NULL;
|
|
}
|
|
}
|
|
|
|
HTSEXT_API void hts_mutexlock(htsmutex * mutex) {
|
|
assertf(mutex != NULL);
|
|
if (*mutex == HTSMUTEX_INIT) { /* must be initialized */
|
|
hts_mutexinit(mutex);
|
|
}
|
|
assertf(*mutex != NULL);
|
|
#ifdef _WIN32
|
|
assertf((*mutex)->handle != NULL);
|
|
WaitForSingleObject((*mutex)->handle, INFINITE);
|
|
#else
|
|
pthread_mutex_lock(&(*mutex)->handle);
|
|
#endif
|
|
}
|
|
|
|
HTSEXT_API void hts_mutexrelease(htsmutex * mutex) {
|
|
assertf(mutex != NULL && *mutex != NULL);
|
|
#ifdef _WIN32
|
|
assertf((*mutex)->handle != NULL);
|
|
ReleaseMutex((*mutex)->handle);
|
|
#else
|
|
pthread_mutex_unlock(&(*mutex)->handle);
|
|
#endif
|
|
}
|
|
|
|
#endif
|