Skip to content

Commit c792861

Browse files
committed
1. spin lock. 2. limited file buffer.
1 parent ac557c9 commit c792861

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

pthread_mutex.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,29 @@
1515

1616

1717
#include <pthread.h>
18+
#include "pthread_spin_lock.h"
1819
//#include "log.h"
1920

2021
class PthreadMutex
2122
{
2223
public:
23-
PthreadMutex(){pthread_mutex_init(&mutexlock, 0);}
24-
virtual ~PthreadMutex(){pthread_mutex_destroy(&mutexlock);}
24+
PthreadMutex(){pthread_spin_init(&mutexlock, 0);}
25+
virtual ~PthreadMutex(){pthread_spin_destroy(&mutexlock);}
2526

2627
void Lock(){
27-
pthread_mutex_lock(&mutexlock);
28+
pthread_spin_lock(&mutexlock);
2829
//MY_METHOD("lock at mutex: 0x%08llx", (unsigned long long)&mutexlock);
2930
}
3031
void Unlock(){
3132
//MY_METHOD("unlock at mutex: 0x%08llx", (unsigned long long)&mutexlock);
32-
pthread_mutex_unlock(&mutexlock);
33+
pthread_spin_unlock(&mutexlock);
3334
}
3435
private:
35-
pthread_mutex_t mutexlock;
36+
pthread_spinlock_t mutexlock;
3637
};
3738

39+
40+
3841
typedef PthreadMutex PthreadRwMutex;
3942

4043
class PthreadGuard

pthread_spin_lock.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Created by lizhen on 2020/11/18.
3+
//
4+
5+
#ifndef PTHREAD_SPIN_LOCK_SHIM
6+
#define PTHREAD_SPIN_LOCK_SHIM
7+
8+
#include <errno.h>
9+
10+
typedef int pthread_spinlock_t;
11+
12+
static inline int pthread_spin_init(pthread_spinlock_t *lock, int pshared) {
13+
__asm__ __volatile__ ("" ::: "memory");
14+
*lock = 0;
15+
return 0;
16+
}
17+
18+
static inline int pthread_spin_destroy(pthread_spinlock_t *lock) {
19+
return 0;
20+
}
21+
22+
static inline int pthread_spin_lock(pthread_spinlock_t *lock) {
23+
while (1) {
24+
if (__sync_bool_compare_and_swap(lock, 0, 1))
25+
return 0;
26+
}
27+
}
28+
29+
static inline int pthread_spin_trylock(pthread_spinlock_t *lock) {
30+
if (__sync_bool_compare_and_swap(lock, 0, 1)) {
31+
return 0;
32+
}
33+
return EBUSY;
34+
}
35+
36+
static inline int pthread_spin_unlock(pthread_spinlock_t *lock) {
37+
__asm__ __volatile__ ("" ::: "memory");
38+
*lock = 0;
39+
return 0;
40+
}
41+
42+
#endif

push.unittest.cmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
adb push libs/x86/test_pthread_mutex /data/local/tmp/ && adb push libs/x86/test_ashmem /data/local/tmp/ && pause

zip/shadow_zip.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,8 @@ FILE* ShadowZip::prepare_file(int _file_index)
654654
return fp_array_[_file_index];
655655
}
656656

657-
//incace of too many file opened, close all except base.apk
657+
//in cace of too many file opened, close all except base.apk
658+
//but if you have only one patch file including everything, you can comment these to get a better performance. The max open files may reach to 100.
658659
for(int i = 1; i < fp_array_.size(); i++)
659660
{
660661
FILE* fp = fp_array_[i];
@@ -674,6 +675,8 @@ FILE* ShadowZip::prepare_file(int _file_index)
674675
MY_METHOD("prepare_file %s -> 0x%08zx", path.c_str(), (size_t)fp);
675676
fp_array_[_file_index] = fp;
676677

678+
//if you have only one patch file including everything, the reading index can be random each time. a smaller buffer size performs better.
679+
//for others, 1024 is also fair enough.
677680
setvbuf( fp , NULL , _IOFBF , 1024);
678681
return fp;
679682
}

0 commit comments

Comments
 (0)