Skip to content

Instantly share code, notes, and snippets.

@wakamoleguy
Last active January 13, 2016 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wakamoleguy/dc9d694e64364ba6f7d2 to your computer and use it in GitHub Desktop.
Save wakamoleguy/dc9d694e64364ba6f7d2 to your computer and use it in GitHub Desktop.
Another update_presentity patch
diff --git a/modules/presence/hash.c b/modules/presence/hash.c
index e67bfc9..8ab5cc1 100644
--- a/modules/presence/hash.c
+++ b/modules/presence/hash.c
@@ -466,6 +466,20 @@ void destroy_phtable(void)
}
/* entry must be locked before calling this function */
+unsigned short contains_phtable(pres_entry_t* p_p, unsigned int hash_code)
+{
+ pres_entry_t* p;
+
+ for ( p=pres_htable[hash_code].entries->next ; p ; p=p->next ) {
+ if(p==p_p) {
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+/* entry must be locked before calling this function */
pres_entry_t* search_phtable(str* pres_uri,int event, unsigned int hash_code)
{
pres_entry_t* p;
@@ -588,6 +602,16 @@ void next_turn_phtable(pres_entry_t* p_p, unsigned int hash_code)
pres_entry_t* p;
lock_get(&pres_htable[hash_code].lock);
+ next_turn_phtable_safe(p_p, hash_code);
+ lock_release(&pres_htable[hash_code].lock);
+ return;
+}
+
+/* entry must be locked before calling this function */
+void next_turn_phtable_safe(pres_entry_t* p_p, unsigned_int hash_code)
+{
+ pres_entry_t* p;
+
for ( p=pres_htable[hash_code].entries->next ; p ; p=p->next ) {
if(p==p_p) {
p->current_turn++;
@@ -597,11 +621,9 @@ void next_turn_phtable(pres_entry_t* p_p, unsigned int hash_code)
}
}
- lock_release(&pres_htable[hash_code].lock);
return;
}
-
int delete_phtable(pres_entry_t* p, unsigned int hash_code)
{
pres_entry_t* prev_p= NULL;
diff --git a/modules/presence/hash.h b/modules/presence/hash.h
index 98ca260..e0684d8 100644
--- a/modules/presence/hash.h
+++ b/modules/presence/hash.h
@@ -143,6 +143,7 @@ typedef struct pres_htable
phtable_t* new_phtable(void);
+unsigned short contains_phtable(pres_entry_t* p_p, unsigned int hash_code);
pres_entry_t* search_phtable(str* pres_uri, int event, unsigned int hash_code);
pres_entry_t* search_phtable_etag(str* pres_uri, int event,
str* etag, unsigned int hash_code);
@@ -154,6 +155,7 @@ pres_entry_t* insert_phtable(str* pres_uri, int event, str* etag, char* sphere,
int update_phtable(struct presentity* presentity, str pres_uri, str body);
void next_turn_phtable(pres_entry_t* p_p, unsigned int hash_code);
+void next_turn_phtable_safe(pres_entry_t* p_p, unsigned int hash_code);
int delete_phtable(pres_entry_t* p, unsigned int hash_code);
int delete_phtable_query(str *pres_uri, int event, str* etag);
diff --git a/modules/presence/presentity.c b/modules/presence/presentity.c
index b287925..c30e974 100644
--- a/modules/presence/presentity.c
+++ b/modules/presence/presentity.c
@@ -410,6 +410,7 @@ int update_presentity(struct sip_msg* msg, presentity_t* presentity, int* sent_r
str* rules_doc= NULL;
str pres_uri= {NULL, 0};
pres_entry_t* p= NULL;
+ pres_entry_t* new_p= NULL;
unsigned int hash_code;
unsigned int turn;
str body = presentity->body;
@@ -561,8 +562,30 @@ int update_presentity(struct sip_msg* msg, presentity_t* presentity, int* sent_r
lock_release(&pres_htable[hash_code].lock);
sleep_us(100);
lock_get(&pres_htable[hash_code].lock);
- p = search_phtable_etag(&pres_uri, presentity->event->evp->parsed,
+ new_p = search_phtable_etag(&pres_uri, presentity->event->evp->parsed,
&presentity->etag, hash_code);
+
+ /* if we are about to throw away the old p,
+ make sure that we increment the turn
+ (because we said we would take one) */
+ if (new_p != p) {
+
+ /* Check that p hasn't been freed or some shit */
+ p = (contains_phtable(p, hash_code)) ? p : NULL;
+
+ if (p && turn!=p->current_turn) {
+ /* Wait your turn before skipping */
+ continue;
+ } else if (p) {
+ /* Skip your turn */
+ next_turn_phtable_safe(p, hash_code);
+ }
+
+ p = new_p;
+ if (p) {
+ turn = p->last_turn++;
+ }
+ }
}
} else {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment